package main
import (
"fmt"
"net"
"io"
"strconv"
)
var (
VER, CONNECT_OK = []byte{5, 0}, []byte{5, 0, 0, 1, 0, 0, 0, 0, 0, 0 }
)
func main() {
listen, err := net.Listen("tcp", "127.0.0.1:9999")
fmt.Println("tcp server start success")
if err != nil {
fmt.Print(err)
return
}
for {
conn, err := listen.Accept()
if err != nil {
fmt.Println("client conn error ", err)
}
defer conn.Close()
go hanlder(conn)
}
}
func hanlder(conn net.Conn) {
bs := make([]byte, 1024)
read(conn, bs)
writer(conn, VER)
read(conn, bs)
var ip string
for i, v := range bs[4:8] {
ip += strconv.Itoa(int(v))
if i != 3 {
ip += "."
}
}
client,err := net.Dial("tcp",ip+":80")
if err != nil {
fmt.Println("create client socket error --> ",err)
return
}
fmt.Println("ip:",ip, "client:",conn.RemoteAddr())
writer(conn, CONNECT_OK)
io.Copy(client,conn)
io.Copy(conn,client)
conn.Close()
client.Close()
fmt.Println(" success ")
//os.Exit(0)
}
func read(conn net.Conn, bs []byte) (len int) {
len, err := conn.Read(bs)
if err != nil && err != io.EOF {
fmt.Print("conn read err ", err)
len = -1
return
}
bs = bs[:len]
fmt.Println("request <--- ", bs, " len ", len)
return
}
func writer(conn net.Conn, bs []byte) (int, error) {
len, err := conn.Write(bs)
fmt.Println("response ---> ", bs)
return len, err
}
有疑问加站长微信联系(非本文作者)