2018-10-07 17:09:45 +00:00
|
|
|
// +build go1.11
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-25 11:17:29 +00:00
|
|
|
"encoding/base64"
|
2020-10-15 22:02:51 +00:00
|
|
|
"encoding/binary"
|
2018-10-07 17:09:45 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2020-04-09 21:11:12 +00:00
|
|
|
"github.com/cbeuw/Cloak/internal/common"
|
2020-04-08 23:34:02 +00:00
|
|
|
"net"
|
2020-04-07 06:05:44 +00:00
|
|
|
"os"
|
|
|
|
|
2018-10-07 17:09:45 +00:00
|
|
|
"github.com/cbeuw/Cloak/internal/client"
|
|
|
|
mux "github.com/cbeuw/Cloak/internal/multiplex"
|
2019-08-02 14:45:33 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-10-07 17:09:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var version string
|
|
|
|
|
|
|
|
func main() {
|
2019-06-09 06:10:22 +00:00
|
|
|
// Should be 127.0.0.1 to listen to a proxy client on this machine
|
2018-10-07 17:09:45 +00:00
|
|
|
var localHost string
|
2019-06-09 06:10:22 +00:00
|
|
|
// port used by proxy clients to communicate with cloak client
|
2018-10-07 17:09:45 +00:00
|
|
|
var localPort string
|
|
|
|
// The ip of the proxy server
|
|
|
|
var remoteHost string
|
|
|
|
// The proxy port,should be 443
|
|
|
|
var remotePort string
|
2019-08-30 21:45:42 +00:00
|
|
|
var proxyMethod string
|
2019-08-14 09:04:27 +00:00
|
|
|
var udp bool
|
2019-06-09 06:10:22 +00:00
|
|
|
var config string
|
2019-07-25 11:17:29 +00:00
|
|
|
var b64AdminUID string
|
2020-05-27 00:06:30 +00:00
|
|
|
var vpnMode bool
|
|
|
|
var tcpFastOpen bool
|
2018-11-22 21:55:23 +00:00
|
|
|
|
2018-10-07 17:09:45 +00:00
|
|
|
log_init()
|
|
|
|
|
2020-04-06 12:07:16 +00:00
|
|
|
ssPluginMode := os.Getenv("SS_LOCAL_HOST") != ""
|
|
|
|
|
2020-04-04 14:29:06 +00:00
|
|
|
verbosity := flag.String("verbosity", "info", "verbosity level")
|
2020-04-06 12:07:16 +00:00
|
|
|
if ssPluginMode {
|
2019-06-09 06:10:22 +00:00
|
|
|
config = os.Getenv("SS_PLUGIN_OPTIONS")
|
2020-05-27 00:06:30 +00:00
|
|
|
flag.BoolVar(&vpnMode, "V", false, "ignored.")
|
|
|
|
flag.BoolVar(&tcpFastOpen, "fast-open", false, "ignored.")
|
2020-04-04 14:29:06 +00:00
|
|
|
flag.Parse() // for verbosity only
|
2018-10-07 17:09:45 +00:00
|
|
|
} else {
|
2019-08-09 21:51:17 +00:00
|
|
|
flag.StringVar(&localHost, "i", "127.0.0.1", "localHost: Cloak listens to proxy clients on this ip")
|
2019-07-31 18:27:05 +00:00
|
|
|
flag.StringVar(&localPort, "l", "1984", "localPort: Cloak listens to proxy clients on this port")
|
2018-10-07 17:09:45 +00:00
|
|
|
flag.StringVar(&remoteHost, "s", "", "remoteHost: IP of your proxy server")
|
|
|
|
flag.StringVar(&remotePort, "p", "443", "remotePort: proxy port, should be 443")
|
2019-08-14 09:04:27 +00:00
|
|
|
flag.BoolVar(&udp, "u", false, "udp: set this flag if the underlying proxy is using UDP protocol")
|
2020-08-17 16:56:05 +00:00
|
|
|
flag.StringVar(&config, "c", "ckclient.json", "config: path to the configuration file or options separated with semicolons")
|
2019-08-30 21:45:42 +00:00
|
|
|
flag.StringVar(&proxyMethod, "proxy", "", "proxy: the proxy method's name. It must match exactly with the corresponding entry in server's ProxyBook")
|
2019-07-25 11:17:29 +00:00
|
|
|
flag.StringVar(&b64AdminUID, "a", "", "adminUID: enter the adminUID to serve the admin api")
|
2018-10-07 17:09:45 +00:00
|
|
|
askVersion := flag.Bool("v", false, "Print the version number")
|
|
|
|
printUsage := flag.Bool("h", false, "Print this message")
|
2020-04-06 12:07:16 +00:00
|
|
|
|
|
|
|
// commandline arguments overrides json
|
|
|
|
|
2018-10-07 17:09:45 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *askVersion {
|
2019-08-02 14:45:33 +00:00
|
|
|
fmt.Printf("ck-client %s", version)
|
2018-10-07 17:09:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if *printUsage {
|
|
|
|
flag.Usage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-02 14:45:33 +00:00
|
|
|
log.Info("Starting standalone mode")
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 14:29:06 +00:00
|
|
|
lvl, err := log.ParseLevel(*verbosity)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.SetLevel(lvl)
|
|
|
|
|
2020-04-06 12:07:16 +00:00
|
|
|
rawConfig, err := client.ParseConfig(config)
|
2018-10-07 17:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-06 12:07:16 +00:00
|
|
|
if ssPluginMode {
|
2020-12-07 20:28:03 +00:00
|
|
|
if rawConfig.ProxyMethod == "" {
|
|
|
|
rawConfig.ProxyMethod = "shadowsocks"
|
|
|
|
}
|
2020-04-06 12:07:16 +00:00
|
|
|
// json takes precedence over environment variables
|
|
|
|
// i.e. if json field isn't empty, use that
|
|
|
|
if rawConfig.RemoteHost == "" {
|
|
|
|
rawConfig.RemoteHost = os.Getenv("SS_REMOTE_HOST")
|
|
|
|
}
|
|
|
|
if rawConfig.RemotePort == "" {
|
2020-04-07 06:05:44 +00:00
|
|
|
rawConfig.RemotePort = os.Getenv("SS_REMOTE_PORT")
|
2020-04-06 12:07:16 +00:00
|
|
|
}
|
|
|
|
if rawConfig.LocalHost == "" {
|
|
|
|
rawConfig.LocalHost = os.Getenv("SS_LOCAL_HOST")
|
|
|
|
}
|
|
|
|
if rawConfig.LocalPort == "" {
|
|
|
|
rawConfig.LocalPort = os.Getenv("SS_LOCAL_PORT")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// commandline argument takes precedence over json
|
|
|
|
// if commandline argument is set, use commandline
|
|
|
|
flag.Visit(func(f *flag.Flag) {
|
|
|
|
// manually set ones
|
|
|
|
switch f.Name {
|
|
|
|
case "i":
|
|
|
|
rawConfig.LocalHost = localHost
|
|
|
|
case "l":
|
|
|
|
rawConfig.LocalPort = localPort
|
|
|
|
case "s":
|
|
|
|
rawConfig.RemoteHost = remoteHost
|
|
|
|
case "p":
|
|
|
|
rawConfig.RemotePort = remotePort
|
2020-04-06 12:11:19 +00:00
|
|
|
case "u":
|
|
|
|
rawConfig.UDP = udp
|
2020-04-06 12:07:16 +00:00
|
|
|
case "proxy":
|
|
|
|
rawConfig.ProxyMethod = proxyMethod
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// ones with default values
|
|
|
|
if rawConfig.LocalHost == "" {
|
|
|
|
rawConfig.LocalHost = localHost
|
|
|
|
}
|
|
|
|
if rawConfig.LocalPort == "" {
|
|
|
|
rawConfig.LocalPort = localPort
|
|
|
|
}
|
|
|
|
if rawConfig.RemotePort == "" {
|
|
|
|
rawConfig.RemotePort = remotePort
|
|
|
|
}
|
2019-06-16 13:59:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 21:51:59 +00:00
|
|
|
localConfig, remoteConfig, authInfo, err := rawConfig.ProcessRawConfig(common.RealWorldState)
|
2020-04-06 12:07:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
2019-07-25 11:17:29 +00:00
|
|
|
|
|
|
|
var adminUID []byte
|
|
|
|
if b64AdminUID != "" {
|
|
|
|
adminUID, err = base64.StdEncoding.DecodeString(b64AdminUID)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 12:07:16 +00:00
|
|
|
var seshMaker func() *mux.Session
|
|
|
|
|
2020-04-08 23:34:02 +00:00
|
|
|
d := &net.Dialer{Control: protector, KeepAlive: remoteConfig.KeepAlive}
|
|
|
|
|
2019-07-25 11:17:29 +00:00
|
|
|
if adminUID != nil {
|
2020-04-06 12:07:16 +00:00
|
|
|
log.Infof("API base is %v", localConfig.LocalAddr)
|
|
|
|
authInfo.UID = adminUID
|
2020-10-15 22:02:51 +00:00
|
|
|
authInfo.SessionId = 0
|
2020-04-06 12:07:16 +00:00
|
|
|
remoteConfig.NumConn = 1
|
|
|
|
|
|
|
|
seshMaker = func() *mux.Session {
|
2020-10-15 22:02:51 +00:00
|
|
|
return client.MakeSession(remoteConfig, authInfo, d)
|
2020-04-06 12:07:16 +00:00
|
|
|
}
|
2019-08-02 14:45:33 +00:00
|
|
|
} else {
|
2019-08-14 10:46:33 +00:00
|
|
|
var network string
|
2020-04-06 12:11:19 +00:00
|
|
|
if authInfo.Unordered {
|
2019-08-16 22:20:24 +00:00
|
|
|
network = "UDP"
|
2019-08-14 10:46:33 +00:00
|
|
|
} else {
|
2019-08-16 22:20:24 +00:00
|
|
|
network = "TCP"
|
2020-04-06 12:07:16 +00:00
|
|
|
}
|
|
|
|
log.Infof("Listening on %v %v for %v client", network, localConfig.LocalAddr, authInfo.ProxyMethod)
|
|
|
|
seshMaker = func() *mux.Session {
|
2020-12-19 14:42:10 +00:00
|
|
|
authInfo := authInfo // copy the struct because we are overwriting SessionId
|
2020-10-15 22:02:51 +00:00
|
|
|
// sessionID is usergenerated. There shouldn't be a security concern because the scope of
|
|
|
|
// sessionID is limited to its UID.
|
|
|
|
quad := make([]byte, 4)
|
|
|
|
common.RandRead(authInfo.WorldState.Rand, quad)
|
|
|
|
authInfo.SessionId = binary.BigEndian.Uint32(quad)
|
|
|
|
return client.MakeSession(remoteConfig, authInfo, d)
|
2019-08-14 10:46:33 +00:00
|
|
|
}
|
2019-07-25 11:17:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 12:11:19 +00:00
|
|
|
if authInfo.Unordered {
|
2020-05-23 21:25:17 +00:00
|
|
|
acceptor := func() (*net.UDPConn, error) {
|
|
|
|
udpAddr, _ := net.ResolveUDPAddr("udp", localConfig.LocalAddr)
|
|
|
|
return net.ListenUDP("udp", udpAddr)
|
|
|
|
}
|
|
|
|
|
2020-12-19 14:42:10 +00:00
|
|
|
client.RouteUDP(acceptor, localConfig.Timeout, remoteConfig.Singleplex, seshMaker)
|
2019-08-14 09:04:27 +00:00
|
|
|
} else {
|
2020-04-10 10:07:38 +00:00
|
|
|
listener, err := net.Listen("tcp", localConfig.LocalAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-12-19 14:42:10 +00:00
|
|
|
client.RouteTCP(listener, localConfig.Timeout, remoteConfig.Singleplex, seshMaker)
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
}
|