Skip to content

Commit

Permalink
fix error of server disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
asche910 committed Oct 17, 2019
1 parent ca7cf0e commit c4c74af
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
12 changes: 8 additions & 4 deletions fly/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func NewCipherInstance(secretKey, method string) *Cipher {
entity := cipherMap[method]
if entity == nil {
entity = cipherMap["aes-256-cfb"]
logger.Println("encrypt method: aes-256-cfb")
}else {
logger.Println("encrypt method:", method)
}

key := genKey(secretKey, entity.keyLen)
newIV := IV[:entity.ivLen]
enc := entity.newStream(key, newIV, ENC)
Expand All @@ -88,7 +92,7 @@ func NewCipherInstance(secretKey, method string) *Cipher {
func newAESCFBStream(key, iv []byte, eod EncOrDec) cipher.Stream {
block, err := aes.NewCipher(key)
if err != nil {
logger.Println("aes.NewCipher failed!", err)
logger.Println("aes.NewCipher failed --->", err)
return nil
}
if eod == ENC {
Expand All @@ -103,7 +107,7 @@ func newAESCFBStream(key, iv []byte, eod EncOrDec) cipher.Stream {
func newAESCTRStream(key, iv []byte, eod EncOrDec) cipher.Stream {
block, err := aes.NewCipher(key)
if err != nil {
logger.Println("aes.NewCipher failed!", err)
logger.Println("aes.NewCipher failed --->", err)
return nil
}
return cipher.NewCTR(block, iv)
Expand All @@ -112,7 +116,7 @@ func newAESCTRStream(key, iv []byte, eod EncOrDec) cipher.Stream {
func newChaCha20Stream(key, iv []byte, eod EncOrDec) cipher.Stream {
stream, err := chacha20.NewCipher(iv, key)
if err != nil {
logger.Println("chacha20.NewCipher failed!", err)
logger.Println("chacha20.NewCipher failed --->", err)
return nil
}
return stream
Expand All @@ -125,7 +129,7 @@ func newRC4MD5Stream(key, iv []byte, eod EncOrDec) cipher.Stream {
rc4key := hs.Sum(nil)
stream, err := rc4.NewCipher(rc4key)
if err != nil {
logger.Println("rc4.NewCipher failed!", err)
logger.Println("rc4.NewCipher failed --->", err)
return nil
}
return stream
Expand Down
4 changes: 2 additions & 2 deletions fly/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func NewConn(con net.Conn, cipher *Cipher) *Conn {
func DialWithAddr(server, method, key string, addr []byte) *Conn {
conn, err := net.Dial("tcp", server)
if err != nil {
logger.Println("Dial server failed!", err)
logger.Println("Dial server failed --->", err)
return nil
}
newConn := NewConn(conn, NewCipherInstance(key, method))
if _, err := newConn.Write(addr); err != nil {
logger.Println("write addr to server failed!", err)
logger.Println("write addr to server failed --->", err)
return nil
}
return newConn
Expand Down
2 changes: 1 addition & 1 deletion fly/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func handleClientRequest(client net.Conn) {

if index == -1 {
index = len(b) - 1
logger.Println("parse request error:", string(b[:]))
logger.Println("parse request error --->", string(b[:]))
}
var method, host, address string
_, _ = fmt.Sscanf(string(b[:]), "%s%s", &method, &host)
Expand Down
2 changes: 1 addition & 1 deletion fly/net_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ListenUDP(localPort string) net.Listener {
func CheckPort(port string) string {
portNum, err := strconv.Atoi(port)
if err != nil {
logger.Fatalln("port is not a number!")
logger.Fatalln("port is not a number --->", err)
}
if portNum < 1 || portNum > 65535 {
logger.Fatalln("port should be in range [1,65536)")
Expand Down
3 changes: 3 additions & 0 deletions fly/pac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package fly


38 changes: 26 additions & 12 deletions fly/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,34 @@ func Socks5ForClientByTCP(localPort, serverAddr, method, key string) {
for {
client, err := listener.Accept()
if err != nil {
logger.Println("accept failed!")
logger.Println("accept failed --->", err)
continue
}
logger.Println("client accepted!")

go func() {
buff := make([]byte, 259)
buff := make([]byte, 1024)
n, err := client.Read(buff)
if err != nil {
logger.Println("read handshake request failed!", err)
logger.Println("read handshake request failed --->", err)
return
}
if buff[0] == 0x05 {
if n, err = client.Write([]byte{0x05, 0x00}); err != nil {
logger.Println("write handshake response failed!", err)
logger.Println("write handshake response failed --->", err)
return
}

// read detail request
//if n, err = client.Read(buff); err != nil {
if n, err = io.ReadAtLeast(client, buff, 5); err != nil {
logger.Println("read client quest failed!", err)
logger.Println("read client quest failed --->", err)
return
}
replyBy := []byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
if _, err = client.Write(replyBy); err != nil {
logger.Println("write 'request success' failed!", err)
logger.Println("write 'request success' failed --->", err)
return
}

//
Expand Down Expand Up @@ -121,25 +126,26 @@ func Socks5ForServerByTCP(localPort, method, key string) {
logger.Println("waiting...")
client, err := listener.Accept()
if err != nil {
logger.Println("server accept failed!", err)
logger.Println("server accept failed --->", err)
continue
}
go func() {
buff := make([]byte, 1024)
conn := NewConn(client, NewCipherInstance(key, method))
n, err := conn.Read(buff)
if err != nil {
logger.Println("read target address failed!", err)
logger.Println("read target address failed --->", err)
return
}

host, port := parseSocksRequest(buff[:n], n)
logger.Printf("target server %s:%s\n", host, port)
//logger.Printf("target server ------\n%s:%s\n------\n%d\n+++++++\n", host, port, buff[:n])
logger.Printf("target server ------\n%s:%s\n------\n", host, port)

// dial the target server
server, err := net.Dial("tcp", net.JoinHostPort(host, port))
if err != nil {
logger.Println("dial target server failed!", err)
logger.Println("dial target server failed --->", err)
return
}
go RelayTraffic(server, conn)
Expand Down Expand Up @@ -240,15 +246,23 @@ func Socks5ForServerByUDP(localPort string) {

// parse socks5 request for target host and port
func parseSocksRequest(data []byte, n int) (string, string) {
// TODO sometimes there are some nums, such as '22 3 1 2 0 1 0 1 252...' after port. why?
var host, port string
var p1, p2 byte
switch data[3] {
case 0x01: // IPV4 address
host = net.IPv4(data[4], data[5], data[6], data[7]).String()
p1 = data[8]
p2 = data[9]
case 0x03: // domain
host = string(data[5 : n-2]) // data[4] stands for the length of domain
host = string(data[5 : 5+data[4]]) // data[4] stands for the length of domain
p1 = data[data[4]+5]
p2 = data[data[4]+6]
case 0x04: // IPV6 address
host = net.IP{data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[16], data[17], data[18], data[19]}.String()
p1 = data[20]
p2 = data[21]
}
port = strconv.Itoa(int(data[n-2])<<8 | int(data[n-1]))
port = strconv.Itoa(int(p1)<<8 | int(p2))
return host, port
}

0 comments on commit c4c74af

Please sign in to comment.