Skip to content
This repository was archived by the owner on Jun 7, 2024. It is now read-only.

Commit 86cb5bb

Browse files
committed
feat: support SOCKS5 (prev)
1 parent e3b1d6e commit 86cb5bb

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module fastgit.org/sniproxygo
2+
3+
go 1.18
4+
5+
require (
6+
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
7+
gopkg.in/yaml.v2 v2.4.0 // indirect
8+
)

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
golang.org/x/net v0.0.0-20220812174116-3211cb980234 h1:RDqmgfe7SvlMWoqC3xwQ2blLO3fcWcxMa3eBLRdRW7E=
2+
golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
3+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
5+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

interfaces.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"net"
5+
6+
"golang.org/x/net/proxy"
7+
)
8+
9+
var proxyDialer proxy.Dialer = nil
10+
11+
func GetDialer(isSocks5 bool) proxy.Dialer {
12+
if isSocks5 {
13+
return &net.Dialer{}
14+
}
15+
if proxyDialer != nil {
16+
return proxyDialer
17+
18+
}
19+
var err error
20+
proxyDialer, err = proxy.SOCKS5("tcp", "127.0.0.1:4000", nil, proxy.Direct)
21+
if err != nil {
22+
return proxyDialer
23+
}
24+
panic(err)
25+
}

main.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"flag"
66
"fmt"
7-
"gopkg.in/yaml.v2"
87
"io"
98
"io/ioutil"
109
"net"
@@ -13,6 +12,8 @@ import (
1312
"strings"
1413
"syscall"
1514
"time"
15+
16+
"gopkg.in/yaml.v2"
1617
)
1718

1819
var SNIPort = 443
@@ -41,7 +42,7 @@ func main() {
4142
os.Exit(0)
4243
}
4344
if len(cfg.ForwardRules) <= 0 {
44-
serviceLogger(fmt.Sprintf("No rules found in yaml!"), 31)
45+
serviceLogger("No rules found in yaml!", 31)
4546
os.Exit(0)
4647
}
4748
for _, rule := range cfg.ForwardRules {
@@ -93,7 +94,7 @@ func serve(c net.Conn, raddr string) {
9394
servername := getSNIServerName(buf[:n])
9495

9596
if servername == "" {
96-
serviceDebugger(fmt.Sprintf("No SNI server name found, ignore it"), 31)
97+
serviceDebugger("No SNI server name found, ignore it", 31)
9798
return
9899
}
99100

@@ -108,7 +109,7 @@ func serve(c net.Conn, raddr string) {
108109
func getSNIServerName(buf []byte) string {
109110
n := len(buf)
110111
if n < 5 {
111-
serviceDebugger(fmt.Sprintf("Not tls handshake"), 31)
112+
serviceDebugger("Not tls handshake", 31)
112113
return ""
113114
}
114115

@@ -120,7 +121,7 @@ func getSNIServerName(buf []byte) string {
120121

121122
// tls major version
122123
if buf[1] != 3 {
123-
serviceDebugger(fmt.Sprintf("TLS version < 3 not supported"), 31)
124+
serviceDebugger("TLS version < 3 not supported", 31)
124125
return ""
125126
}
126127

@@ -131,7 +132,7 @@ func getSNIServerName(buf []byte) string {
131132

132133
// handshake message type
133134
if buf[5] != typeClientHello {
134-
serviceDebugger(fmt.Sprintf("Not client hello"), 31)
135+
serviceDebugger("Not client hello", 31)
135136
return ""
136137
}
137138

@@ -142,14 +143,15 @@ func getSNIServerName(buf []byte) string {
142143
// client hello message not include tls header, 5 bytes
143144
ret := msg.unmarshal(buf[5:n])
144145
if !ret {
145-
serviceDebugger(fmt.Sprintf("Parse hello message return false"), 31)
146+
serviceDebugger("Parse hello message return false", 31)
146147
return ""
147148
}
148149
return msg.serverName
149150
}
150151

151152
func forward(conn net.Conn, data []byte, dst string, raddr string) {
152-
backend, err := net.Dial("tcp", dst)
153+
// TODO: FIX
154+
backend, err := GetDialer(true).Dial("tcp", dst)
153155
if err != nil {
154156
serviceLogger(fmt.Sprintf("Couldn't connect to backend, %v", err), 31)
155157
return

0 commit comments

Comments
 (0)