-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Porting ss-redir
to go
#473
base: master
Are you sure you want to change the base?
Changes from all commits
03c8566
f689827
3567425
8e38949
0053b2a
6292c35
f36d956
008567c
f5ef885
f4e7a4a
a10f8b6
74f938b
20f6d66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
script/http | ||
bin | ||
.idea | ||
.vscode/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package main | |
import ( | ||
"flag" | ||
"fmt" | ||
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks" | ||
"io" | ||
"math" | ||
"net" | ||
|
@@ -14,6 +13,8 @@ import ( | |
"strconv" | ||
"strings" | ||
"time" | ||
|
||
ss "github.com/bonafideyan/shadowsocks-go/shadowsocks" | ||
) | ||
|
||
var config struct { | ||
|
@@ -57,7 +58,11 @@ func get(connid int, uri, serverAddr string, rawAddr []byte, cipher *ss.Cipher, | |
}() | ||
tr := &http.Transport{ | ||
Dial: func(_, _ string) (net.Conn, error) { | ||
return ss.DialWithRawAddr(rawAddr, serverAddr, cipher.Copy()) | ||
if cipher != nil { | ||
return ss.DialWithRawAddr(rawAddr, serverAddr, cipher.Copy()) | ||
} | ||
|
||
return ss.DialAsClient(string(rawAddr), serverAddr) | ||
}, | ||
} | ||
|
||
|
@@ -77,8 +82,9 @@ func get(connid int, uri, serverAddr string, rawAddr []byte, cipher *ss.Cipher, | |
} | ||
|
||
func main() { | ||
flag.StringVar(&config.server, "s", "127.0.0.1", "server:port") | ||
flag.IntVar(&config.port, "p", 0, "server:port") | ||
server := flag.String("s", "127.0.0.1", "server:port") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keey the code style |
||
proxy := flag.String("ss", "127.0.0.1", "proxy:port") | ||
flag.IntVar(&config.port, "p", 0, "port") | ||
flag.IntVar(&config.core, "core", 1, "number of CPU cores to use") | ||
flag.StringVar(&config.passwd, "k", "", "password") | ||
flag.StringVar(&config.method, "m", "", "encryption method, use empty string or rc4") | ||
|
@@ -89,8 +95,17 @@ func main() { | |
|
||
flag.Parse() | ||
|
||
if config.server == "" || config.port == 0 || config.passwd == "" || len(flag.Args()) != 1 { | ||
fmt.Printf("Usage: %s -s <server> -p <port> -k <password> <url>\n", os.Args[0]) | ||
config.server = "127.0.0.1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default config can be set with flag |
||
var connectProxy bool | ||
if *server != config.server { | ||
config.server = *server | ||
} else { | ||
config.server = *proxy | ||
connectProxy = true | ||
} | ||
|
||
if config.port == 0 || !connectProxy && config.passwd == "" || len(flag.Args()) != 1 { | ||
fmt.Printf("Usage: %s -s[s] <server> -p <port> -k <password> <url>\n", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
|
||
|
@@ -104,11 +119,6 @@ func main() { | |
uri = "http://" + uri | ||
} | ||
|
||
cipher, err := ss.NewCipher(config.method, config.passwd) | ||
if err != nil { | ||
fmt.Println("Error creating cipher:", err) | ||
os.Exit(1) | ||
} | ||
serverAddr := net.JoinHostPort(config.server, strconv.Itoa(config.port)) | ||
|
||
parsedURL, err := url.Parse(uri) | ||
|
@@ -122,10 +132,23 @@ func main() { | |
} else { | ||
host = parsedURL.Host | ||
} | ||
// fmt.Println(host) | ||
rawAddr, err := ss.RawAddr(host) | ||
if err != nil { | ||
panic("Error getting raw address.") | ||
|
||
rawAddr := []byte(host) | ||
var cipher *ss.Cipher | ||
if !connectProxy { | ||
if config.method == "" { | ||
config.method = "aes-256-cfb" | ||
} | ||
|
||
cipher, err = ss.NewCipher(config.method, config.passwd) | ||
if err != nil { | ||
fmt.Println("Error creating cipher:", err) | ||
os.Exit(1) | ||
} | ||
rawAddr, err = ss.RawAddr(host) | ||
if err != nil { | ||
panic("Error getting raw address.") | ||
} | ||
} | ||
|
||
done := make(chan []time.Duration) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,12 @@ import ( | |
"strings" | ||
"time" | ||
|
||
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks" | ||
ss "github.com/bonafideyan/shadowsocks-go/shadowsocks" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls fix the dependency There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
) | ||
|
||
var debug ss.DebugLog | ||
|
||
var ( | ||
debug ss.DebugLog | ||
|
||
errAddrType = errors.New("socks addr type not supported") | ||
errVer = errors.New("socks version not supported") | ||
errMethod = errors.New("socks only support 1 method now") | ||
|
@@ -246,6 +246,7 @@ func connectToServer(serverId int, rawaddr []byte, addr string) (remote *ss.Conn | |
} | ||
return nil, err | ||
} | ||
|
||
debug.Printf("connected to %s via %s\n", addr, se.server) | ||
servers.failCnt[serverId] = 0 | ||
return | ||
|
@@ -280,7 +281,7 @@ func createServerConn(rawaddr []byte, addr string) (remote *ss.Conn, err error) | |
return nil, err | ||
} | ||
|
||
func handleConnection(conn net.Conn) { | ||
func handleConnection(conn net.Conn, redir bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need a new function called redirectConnection |
||
if debug { | ||
debug.Printf("socks connect from %s\n", conn.RemoteAddr().String()) | ||
} | ||
|
@@ -291,23 +292,37 @@ func handleConnection(conn net.Conn) { | |
} | ||
}() | ||
|
||
var err error = nil | ||
if err = handShake(conn); err != nil { | ||
log.Println("socks handshake:", err) | ||
return | ||
} | ||
rawaddr, addr, err := getRequest(conn) | ||
if err != nil { | ||
log.Println("error getting request:", err) | ||
return | ||
} | ||
// Sending connection established message immediately to client. | ||
// This some round trip time for creating socks connection with the client. | ||
// But if connection failed, the client will get connection reset error. | ||
_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43}) | ||
if err != nil { | ||
debug.Println("send connection confirmation:", err) | ||
return | ||
var rawaddr, buf []byte | ||
var addr string | ||
var n int | ||
if !redir { | ||
var err error = nil | ||
if err = handShake(conn); err != nil { | ||
log.Println("socks handshake:", err) | ||
return | ||
} | ||
rawaddr, addr, err = getRequest(conn) | ||
if err != nil { | ||
log.Println("error getting request:", err) | ||
return | ||
} | ||
// Sending connection established message immediately to client. | ||
// This some round trip time for creating socks connection with the client. | ||
// But if connection failed, the client will get connection reset error. | ||
_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43}) | ||
if err != nil { | ||
debug.Println("send connection confirmation:", err) | ||
return | ||
} | ||
} else { | ||
var err error | ||
buf = ss.GetLeakyBuf() | ||
n, err = conn.Read(buf) | ||
if err != nil { | ||
println(err.Error()) | ||
} | ||
|
||
rawaddr, addr = ss.GetOriginalDst(&conn, string(buf[:n])) | ||
} | ||
|
||
remote, err := createServerConn(rawaddr, addr) | ||
|
@@ -323,13 +338,13 @@ func handleConnection(conn net.Conn) { | |
} | ||
}() | ||
|
||
go ss.PipeThenClose(conn, remote, nil) | ||
ss.PipeThenClose(remote, conn, nil) | ||
go ss.PipeThenClose(conn, remote, nil, buf, n) | ||
ss.PipeThenClose(remote, conn, nil, nil, 0) | ||
closed = true | ||
debug.Println("closed connection to", addr) | ||
} | ||
|
||
func run(listenAddr string) { | ||
func run(listenAddr string, redir bool) { | ||
ln, err := net.Listen("tcp", listenAddr) | ||
if err != nil { | ||
log.Fatal(err) | ||
|
@@ -341,7 +356,7 @@ func run(listenAddr string) { | |
log.Println("accept:", err) | ||
continue | ||
} | ||
go handleConnection(conn) | ||
go handleConnection(conn, redir) | ||
} | ||
} | ||
|
||
|
@@ -407,8 +422,10 @@ func main() { | |
var configFile, cmdServer, cmdURI string | ||
var cmdConfig ss.Config | ||
var printVer bool | ||
var redirect bool | ||
|
||
flag.BoolVar(&printVer, "version", false, "print version") | ||
flag.BoolVar(&redirect, "redirect", false, "Redirect normal request to socks server.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
flag.StringVar(&configFile, "c", "config.json", "specify config file") | ||
flag.StringVar(&cmdServer, "s", "", "server address") | ||
flag.StringVar(&cmdConfig.LocalAddress, "b", "", "local address, listen only to this address if specified") | ||
|
@@ -477,5 +494,6 @@ func main() { | |
} | ||
|
||
parseServerConfig(config) | ||
run(config.LocalAddress + ":" + strconv.Itoa(config.LocalPort)) | ||
run(config.LocalAddress+":"+strconv.Itoa(config.LocalPort), redirect) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/bonafideyan/shadowsocks-go | ||
|
||
go 1.13 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix your dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
your can add your branch as a reference for git
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't know how to do it, could you please give me some steps in detail?