写好了不能用,好像是无法传输数据
#客户端
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)
}
}
'''
更多评论