Cloak/cmd/ck-server/ck-server.go

169 lines
4.1 KiB
Go
Raw Normal View History

2018-10-09 15:07:54 +00:00
package main
import (
"flag"
"fmt"
2020-04-09 21:11:12 +00:00
"github.com/cbeuw/Cloak/internal/common"
"github.com/cbeuw/Cloak/internal/server"
log "github.com/sirupsen/logrus"
2018-10-09 15:07:54 +00:00
"net"
"net/http"
2019-07-25 21:06:33 +00:00
_ "net/http/pprof"
2018-10-09 15:07:54 +00:00
"os"
"runtime"
2018-10-09 15:07:54 +00:00
"strings"
)
var version string
2020-04-09 12:26:23 +00:00
func parseBindAddr(bindAddrs []string) ([]net.Addr, error) {
var addrs []net.Addr
for _, addr := range bindAddrs {
bindAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return nil, err
}
addrs = append(addrs, bindAddr)
}
return addrs, nil
}
2018-10-09 15:07:54 +00:00
func main() {
var config string
2018-10-09 15:07:54 +00:00
var pluginMode bool
if os.Getenv("SS_LOCAL_HOST") != "" && os.Getenv("SS_LOCAL_PORT") != "" {
pluginMode = true
config = os.Getenv("SS_PLUGIN_OPTIONS")
2018-10-09 15:07:54 +00:00
} else {
flag.StringVar(&config, "c", "server.json", "config: path to the configuration file or its content")
2018-10-09 15:07:54 +00:00
askVersion := flag.Bool("v", false, "Print the version number")
printUsage := flag.Bool("h", false, "Print this message")
2018-12-17 22:12:38 +00:00
genUID := flag.Bool("u", false, "Generate a UID")
genKeyPair := flag.Bool("k", false, "Generate a pair of public and private key, output in the format of pubkey,pvkey")
2019-01-21 11:49:01 +00:00
pprofAddr := flag.String("d", "", "debug use: ip:port to be listened by pprof profiler")
2019-08-09 21:51:17 +00:00
verbosity := flag.String("verbosity", "info", "verbosity level")
2019-01-21 11:49:01 +00:00
2018-10-09 15:07:54 +00:00
flag.Parse()
if *askVersion {
2019-08-02 14:45:33 +00:00
fmt.Printf("ck-server %s", version)
2018-10-09 15:07:54 +00:00
return
}
if *printUsage {
flag.Usage()
return
}
2018-12-17 22:12:38 +00:00
if *genUID {
fmt.Println(generateUID())
return
}
if *genKeyPair {
pub, pv := generateKeyPair()
fmt.Printf("%v,%v", pub, pv)
return
}
2018-10-09 15:07:54 +00:00
2019-01-21 11:49:01 +00:00
if *pprofAddr != "" {
runtime.SetBlockProfileRate(5)
go func() {
2019-08-02 14:45:33 +00:00
log.Info(http.ListenAndServe(*pprofAddr, nil))
}()
2019-08-02 14:45:33 +00:00
log.Infof("pprof listening on %v", *pprofAddr)
2019-01-21 11:49:01 +00:00
}
2019-08-09 21:51:17 +00:00
lvl, err := log.ParseLevel(*verbosity)
if err != nil {
log.Fatal(err)
}
log.SetLevel(lvl)
log.Infof("Starting standalone mode")
2018-10-09 15:07:54 +00:00
}
2018-11-07 21:16:13 +00:00
2020-04-09 12:26:23 +00:00
raw, err := server.ParseConfig(config)
2018-10-09 15:07:54 +00:00
if err != nil {
log.Fatalf("Configuration file error: %v", err)
}
2020-04-09 12:26:23 +00:00
bindAddr, err := parseBindAddr(raw.BindAddr)
if err != nil {
2020-08-17 16:56:05 +00:00
log.Fatalf("unable to parse BindAddr: %v", err)
2020-04-09 12:26:23 +00:00
}
if !pluginMode && len(bindAddr) == 0 {
https, _ := net.ResolveTCPAddr("tcp", ":443")
http, _ := net.ResolveTCPAddr("tcp", ":80")
2020-04-09 12:26:23 +00:00
bindAddr = []net.Addr{https, http}
}
// when cloak is started as a shadowsocks plugin
if pluginMode {
ssLocalHost := os.Getenv("SS_LOCAL_HOST")
ssLocalPort := os.Getenv("SS_LOCAL_PORT")
2020-04-09 12:26:23 +00:00
raw.ProxyBook["shadowsocks"] = []string{"tcp", net.JoinHostPort(ssLocalHost, ssLocalPort)}
2020-04-09 12:26:23 +00:00
ssRemoteHost := os.Getenv("SS_REMOTE_HOST")
ssRemotePort := os.Getenv("SS_REMOTE_PORT")
var ssBind string
// When listening on an IPv6 and IPv4, SS gives REMOTE_HOST as e.g. ::|0.0.0.0
v4nv6 := len(strings.Split(ssRemoteHost, "|")) == 2
if v4nv6 {
ssBind = ":" + ssRemotePort
} else {
ssBind = net.JoinHostPort(ssRemoteHost, ssRemotePort)
}
ssBindAddr, err := net.ResolveTCPAddr("tcp", ssBind)
if err != nil {
log.Fatalf("unable to resolve bind address provided by SS: %v", err)
}
shouldAppend := true
2020-04-09 12:26:23 +00:00
for i, addr := range bindAddr {
if addr.String() == ssBindAddr.String() {
shouldAppend = false
}
if addr.String() == ":"+ssRemotePort { // already listening on all interfaces
shouldAppend = false
}
if addr.String() == "0.0.0.0:"+ssRemotePort || addr.String() == "[::]:"+ssRemotePort {
// if config listens on one ip version but ss wants to listen on both,
// listen on both
if ssBindAddr.String() == ":"+ssRemotePort {
shouldAppend = true
2020-04-09 12:26:23 +00:00
bindAddr[i] = ssBindAddr
}
}
}
if shouldAppend {
2020-04-09 12:26:23 +00:00
bindAddr = append(bindAddr, ssBindAddr)
}
}
2020-04-09 21:11:12 +00:00
sta, err := server.InitState(raw, common.RealWorldState)
2020-04-09 12:26:23 +00:00
if err != nil {
log.Fatalf("unable to initialise server state: %v", err)
}
listen := func(bindAddr net.Addr) {
listener, err := net.Listen("tcp", bindAddr.String())
log.Infof("Listening on %v", bindAddr)
2018-10-09 15:07:54 +00:00
if err != nil {
log.Fatal(err)
}
2020-04-10 10:07:38 +00:00
server.Serve(listener, sta)
2018-10-09 15:07:54 +00:00
}
2020-04-09 12:26:23 +00:00
for i, addr := range bindAddr {
if i != len(bindAddr)-1 {
go listen(addr)
2018-10-09 15:07:54 +00:00
} else {
listen(addr)
2018-10-09 15:07:54 +00:00
}
}
}