forked from shadowsocks/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
179 additions
and
23 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
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,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
) | ||
|
||
var logger = log.New(os.Stderr, "", log.Lshortfile|log.LstdFlags) | ||
|
||
func logf(f string, v ...interface{}) { | ||
if config.Verbose { | ||
logger.Output(2, fmt.Sprintf(f, v...)) | ||
} | ||
} | ||
|
||
type logHelper struct { | ||
prefix string | ||
} | ||
|
||
func (l *logHelper) Write(p []byte) (n int, err error) { | ||
if config.Verbose { | ||
logger.Printf("%s%s\n", l.prefix, p) | ||
return len(p), nil | ||
} | ||
return | ||
} | ||
|
||
func newLogHelper(prefix string) *logHelper { | ||
return &logHelper{prefix} | ||
} |
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
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,88 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func startPlugin(plugin, pluginOpts, ssAddr string, isServer bool) (newAddr string, err error) { | ||
logf("starting plugin (%s) with option (%s)....", plugin, pluginOpts) | ||
freePort, err := getFreePort() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to fetch an unused port for plugin (%v)", err) | ||
} | ||
localHost := "127.0.0.1" | ||
ssHost, ssPort, err := net.SplitHostPort(ssAddr) | ||
if err != nil { | ||
return "", err | ||
} | ||
newAddr = localHost + ":" + freePort | ||
if isServer { | ||
if ssHost == "" { | ||
ssHost = "0.0.0.0" | ||
} | ||
logf("plugin (%s) will listen on %s:%s", plugin, ssHost, ssPort) | ||
} else { | ||
logf("plugin (%s) will listen on %s:%s", plugin, localHost, freePort) | ||
} | ||
err = execPlugin(plugin, pluginOpts, ssHost, ssPort, localHost, freePort) | ||
return | ||
} | ||
|
||
func execPlugin(plugin, pluginOpts, remoteHost, remotePort, localHost, localPort string) error { | ||
if fileExists(plugin) { | ||
plugin = "./" + plugin | ||
} | ||
logH := newLogHelper("[" + plugin + "]: ") | ||
env := append(os.Environ(), | ||
"SS_REMOTE_HOST="+remoteHost, | ||
"SS_REMOTE_PORT="+remotePort, | ||
"SS_LOCAL_HOST="+localHost, | ||
"SS_LOCAL_PORT="+localPort, | ||
"SS_PLUGIN_OPTIONS="+pluginOpts, | ||
) | ||
cmd := &exec.Cmd{ | ||
Path: plugin, | ||
Args: []string{plugin}, | ||
Env: env, | ||
Stdout: logH, | ||
Stderr: logH, | ||
} | ||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
go func() { | ||
if err := cmd.Wait(); err != nil { | ||
logf("plugin exited (%v)\n", err) | ||
os.Exit(2) | ||
} | ||
logf("plugin exited\n") | ||
os.Exit(0) | ||
}() | ||
return nil | ||
} | ||
|
||
func fileExists(filename string) bool { | ||
info, err := os.Stat(filename) | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
return !info.IsDir() | ||
} | ||
|
||
func getFreePort() (string, error) { | ||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
l, err := net.ListenTCP("tcp", addr) | ||
if err != nil { | ||
return "", err | ||
} | ||
port := fmt.Sprintf("%d", l.Addr().(*net.TCPAddr).Port) | ||
l.Close() | ||
return port, nil | ||
} |