-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 808c77b
Showing
5 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package main | ||
|
||
import ( | ||
"shadowsocks" | ||
"net" | ||
"bytes" | ||
"log" | ||
"fmt" | ||
) | ||
|
||
func handleConnection(conn net.Conn, encryptTable, decryptTable []byte, server string) { | ||
log.Printf("socks connect from %s\n", conn.RemoteAddr().String()) | ||
b := make([]byte, 262) | ||
var err error = nil | ||
var hasError = false | ||
for { | ||
var _ int | ||
buf := make([]byte, 4096) | ||
_, err = conn.Read(b) | ||
if err != nil { | ||
hasError = true | ||
break | ||
} | ||
conn.Write([]byte{0x05, 0x00}) | ||
_, err = conn.Read(buf) | ||
mode := buf[1] | ||
if mode != 1 { | ||
hasError = true | ||
log.Println("mode != 1") | ||
break | ||
} | ||
var addr string | ||
addrType := buf[3] | ||
var addrToSend []byte | ||
if addrType == 1 { | ||
var addrIp net.IP = make(net.IP, 4) | ||
copy(addrIp, buf[4:8]) | ||
addr = addrIp.String() | ||
addrToSend = buf[3:10] | ||
} else if addrType == 3 { | ||
addrLen := buf[4] | ||
sb := bytes.NewBuffer(buf[5:5 + addrLen]) | ||
addr = sb.String() | ||
addrToSend = buf[3:5 + addrLen + 2] | ||
} else { | ||
hasError = true | ||
log.Println("unsurpported addr type") | ||
break | ||
} | ||
log.Println("connecting ", addr) | ||
conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) | ||
var remote net.Conn | ||
remote, err = net.Dial("tcp", server) | ||
if err != nil { | ||
hasError = true | ||
break | ||
} | ||
_, err = remote.Write(shadowsocks.Encrypt(encryptTable, addrToSend)) | ||
if err != nil { | ||
hasError = true | ||
break | ||
} | ||
c := make(chan int, 2) | ||
go shadowsocks.Pipe(conn, remote, encryptTable, c) | ||
go shadowsocks.Pipe(remote, conn, decryptTable, c) | ||
<-c // close the other connection whenever one connection is closed | ||
log.Println("closing") | ||
err = conn.Close() | ||
err1 := remote.Close() | ||
if err == nil { | ||
err = err1 | ||
} | ||
break | ||
} | ||
if err != nil || hasError { | ||
if err != nil { | ||
log.Println("error ", err) | ||
} | ||
err = conn.Close() | ||
if err != nil { | ||
log.Println("close:", err) | ||
} | ||
return | ||
} | ||
|
||
} | ||
|
||
func run(encryptTable, decryptTable []byte, port int, server string) { | ||
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
log.Printf("starting server at port %d ...\n", port) | ||
for { | ||
conn, err := ln.Accept() | ||
if err != nil { | ||
log.Println("accept:", err) | ||
continue | ||
} | ||
go handleConnection(net.Conn(conn), encryptTable, decryptTable, server) | ||
} | ||
} | ||
|
||
func main() { | ||
encyrptTable, decryptTable := shadowsocks.GetTable("foobar!") | ||
run(encyrptTable, decryptTable, 1080, "127.0.0.1:8388") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package shadowsocks | ||
|
||
import ( | ||
"crypto/md5" | ||
"io" | ||
"encoding/binary" | ||
"bytes" | ||
) | ||
|
||
func GetTable(key string) (encryptTable []byte, decryptTable []byte) { | ||
encryptTable = make([]byte, 256) | ||
decryptTable = make([]byte, 256) | ||
table := make([]int, 256) | ||
|
||
h := md5.New() | ||
io.WriteString(h, key) | ||
|
||
s := h.Sum(nil) | ||
|
||
var a int64 | ||
buf := bytes.NewBuffer(s) | ||
binary.Read(buf, binary.LittleEndian, &a) | ||
for i := 0; i < 256; i++ { | ||
table[i] = i | ||
} | ||
for i := 1; i < 1024; i++ { | ||
table = Sort(table, func(x, y int) int { | ||
return int(a%int64(x + i) - a%int64(y + i)) | ||
}) | ||
} | ||
for i := 0; i < 256; i++ { | ||
encryptTable[i] = byte(table[i]) | ||
} | ||
for i := 0; i < 256; i++ { | ||
decryptTable[encryptTable[i]] = byte(i) | ||
} | ||
|
||
return | ||
} | ||
|
||
func Encrypt(table []byte, buf []byte) []byte { | ||
var result = make([]byte, len(buf)) | ||
for i := 0; i < len(buf); i++ { | ||
result[i] = table[buf[i]] | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package shadowsocks | ||
|
||
func merge(left, right []int, comparison func (int, int) int) []int { | ||
result := make([]int, len(left) + len(right)) | ||
l, r := 0, 0 | ||
for (l < len(left)) && (r < len(right)) { | ||
if comparison(left[l], right[r]) <= 0 { | ||
result[l + r] = left[l] | ||
l++ | ||
} else { | ||
result[l + r] = right[r] | ||
r++ | ||
} | ||
} | ||
for (l < len(left)) { | ||
result[l + r] = left[l] | ||
l++ | ||
} | ||
for (r < len(right)) { | ||
result[l + r] = right[r] | ||
r++ | ||
} | ||
return result | ||
} | ||
|
||
func Sort(arr []int, comparison func (int, int) int) []int { | ||
if len(arr) < 2 { | ||
return arr | ||
} | ||
var middle int = int(len(arr)/2) | ||
return merge(Sort(arr[0:middle], comparison), Sort(arr[middle:], comparison), comparison) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package shadowsocks | ||
|
||
import ( | ||
"net" | ||
"log" | ||
) | ||
|
||
func Pipe(src net.Conn, dst net.Conn, table []byte, end chan int) { | ||
buf := make([]byte, 4096) | ||
for { | ||
num, err := src.Read(buf) | ||
if err == nil { | ||
_, err := dst.Write(Encrypt(table, buf[0:num])) | ||
if err != nil { | ||
log.Println("write:", err) | ||
end <- 1 | ||
return | ||
} | ||
} else { | ||
log.Println("read:", err) | ||
end <- 1 | ||
return | ||
} | ||
if num == 0 { | ||
end <- 1 | ||
return | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"shadowsocks" | ||
"log" | ||
) | ||
|
||
func main() { | ||
|
||
target1 := []byte{60, 53, 84, 138, 217, 94, 88, 23, 39, 242, 219, 35, 12, 157, 165, 181, 255, 143, 83, 247, 162, 16, 31, 209, 190, 171, 115, 65, 38, 41, 21, 245, 236, 46, 121, 62, 166, 233, 44, 154, 153, 145, 230, 49, 128, 216, 173, 29, 241, 119, 64, 229, 194, 103, 131, 110, 26, 197, 218, 59, 204, 56, 27, 34, 141, 221, 149, 239, 192, 195, 24, 155, 170, 183, 11, 254, 213, 37, 137, 226, 75, 203, 55, 19, 72, 248, 22, 129, 33, 175, 178, 10, 198, 71, 77, 36, 113, 167, 48, 2, 117, 140, 142, 66, 199, 232, 243, 32, 123, 54, 51, 82, 57, 177, 87, 251, 150, 196, 133, 5, 253, 130, 8, 184, 14, 152, 231, 3, 186, 159, 76, 89, 228, 205, 156, 96, 163, 146, 18, 91, 132, 85, 80, 109, 172, 176, 105, 13, 50, 235, 127, 0, 189, 95, 98, 136, 250, 200, 108, 179, 211, 214, 106, 168, 78, 79, 74, 210, 30, 73, 201, 151, 208, 114, 101, 174, 92, 52, 120, 240, 15, 169, 220, 182, 81, 224, 43, 185, 40, 99, 180, 17, 212, 158, 42, 90, 9, 191, 45, 6, 25, 4, 222, 67, 126, 1, 116, 124, 206, 69, 61, 7, 68, 97, 202, 63, 244, 20, 28, 58, 93, 134, 104, 144, 227, 147, 102, 118, 135, 148, 47, 238, 86, 112, 122, 70, 107, 215, 100, 139, 223, 225, 164, 237, 111, 125, 207, 160, 187, 246, 234, 161, 188, 193, 249, 252 } | ||
target2 := []byte{ 151, 205, 99, 127, 201, 119, 199, 211, 122, 196, 91, 74, 12, 147, 124, 180, 21, 191, 138, 83, 217, 30, 86, 7, 70, 200, 56, 62, 218, 47, 168, 22, 107, 88, 63, 11, 95, 77, 28, 8, 188, 29, 194, 186, 38, 198, 33, 230, 98, 43, 148, 110, 177, 1, 109, 82, 61, 112, 219, 59, 0, 210, 35, 215, 50, 27, 103, 203, 212, 209, 235, 93, 84, 169, 166, 80, 130, 94, 164, 165, 142, 184, 111, 18, 2, 141, 232, 114, 6, 131, 195, 139, 176, 220, 5, 153, 135, 213, 154, 189, 238, 174, 226, 53, 222, 146, 162, 236, 158, 143, 55, 244, 233, 96, 173, 26, 206, 100, 227, 49, 178, 34, 234, 108, 207, 245, 204, 150, 44, 87, 121, 54, 140, 118, 221, 228, 155, 78, 3, 239, 101, 64, 102, 17, 223, 41, 137, 225, 229, 66, 116, 171, 125, 40, 39, 71, 134, 13, 193, 129, 247, 251, 20, 136, 242, 14, 36, 97, 163, 181, 72, 25, 144, 46, 175, 89, 145, 113, 90, 159, 190, 15, 183, 73, 123, 187, 128, 248, 252, 152, 24, 197, 68, 253, 52, 69, 117, 57, 92, 104, 157, 170, 214, 81, 60, 133, 208, 246, 172, 23, 167, 160, 192, 76, 161, 237, 45, 4, 58, 10, 182, 65, 202, 240, 185, 241, 79, 224, 132, 51, 42, 126, 105, 37, 250, 149, 32, 243, 231, 67, 179, 48, 9, 106, 216, 31, 249, 19, 85, 254, 156, 115, 255, 120, 75, 16 } | ||
normal := true | ||
encyrptTable, decryptTable := shadowsocks.GetTable("foobar!") | ||
for i := 0; i < 256; i++ { | ||
if target1[i] != encyrptTable[i] { | ||
normal = false | ||
break | ||
} | ||
if target2[i] != decryptTable[i] { | ||
normal = false | ||
break | ||
} | ||
} | ||
if normal { | ||
log.Println("test passed") | ||
} else { | ||
log.Println("test failed") | ||
} | ||
|
||
} |