go语言编写socks5代理服务器中遇到的问题

WilliamColton · · 2127 次点击 · 开始浏览    置顶
写好了不能用,好像是无法传输数据 #客户端 local.go: ''' package main import ( "EncryptionAndDecryption" "fmt" "net" ) func main() { key := []byte{0, 6, 80, 90, 60, 255, 67, 45, 67, 80, 234, 255, 200, 100, 0, 245} l, _ := net.Listen("tcp", ":5678") for { conn, _ := l.Accept() server, _ := net.Dial("tcp", ":7890") go Do(conn, server, key) } } func Do(conn, server net.Conn, key []byte) { go func() { data := make([]byte, 4096) for { n, err := conn.Read(data) if err != nil { fmt.Println(err, "conn err") continue } endata := Encrytion.Endata(data[:n], key) server.Write(endata) } }() endata := make([]byte, 4096) for { n, err := server.Read(endata) if err != nil { fmt.Println(err, "server err") continue } dedata := Encrytion.Dedata(endata[:n], key) conn.Write(dedata) } } ''' //服务端 server.go: ''' package main import ( "EncryptionAndDecryption" "encoding/binary" "fmt" "net" ) func main() { key := []byte{0, 6, 80, 90, 60, 255, 67, 45, 67, 80, 234, 255, 200, 100, 0, 245} l, _ := net.Listen("tcp", ":7890") for { conn, _ := l.Accept() go Do(conn, key) } } func Do(conn net.Conn, key []byte) { data := make([]byte, 4096) conn.Read(data) a := Encrytion.Endata([]byte{0x05, 0x00}, key) conn.Write(a) n, _ := conn.Read(data) dedata := Encrytion.Dedata(data[:n], key) atyp := dedata[3] addr := "" AddrPort := "" addrlen := 0 switch atyp { case 1: addr = fmt.Sprintf("%d.%d.%d.%d", dedata[4], dedata[5], dedata[6], dedata[7]) port := binary.BigEndian.Uint16(dedata[8:10]) AddrPort = fmt.Sprintf("%s:%d", addr, port) case 3: addrlen = int(dedata[4]) addr = string(dedata[5 : 5+addrlen]) port := binary.BigEndian.Uint16(dedata[5+addrlen : 7+addrlen]) AddrPort = fmt.Sprintf("%s:%d", addr, port) case 4: fmt.Println("sorry,no IPv6!") return } fmt.Println(AddrPort, len(AddrPort)) to, err := net.Dial("tcp", AddrPort) if err != nil { fmt.Println(err) return } go func() { endata := make([]byte, 4096) for { n, err := conn.Read(endata) if err != nil { fmt.Println(err, "conn err") break } dedata := Encrytion.Dedata(endata[:n], key) fmt.Println(dedata) to.Write(dedata) } }() data = make([]byte, 4096) for { n, err := to.Read(data) if err != nil { fmt.Println(err, "to err") break } endata := Encrytion.Endata(data[:n], key) conn.Write(endata) } } '''

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077

2127 次点击  ∙  1 赞  
加入收藏 微博
9 回复  |  直到 2021-08-11 19:39:32
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传