Cloak/cmd/ck-client/ck-client.go

214 lines
5.2 KiB
Go
Raw Normal View History

2018-10-07 17:09:45 +00:00
// +build go1.11
package main
import (
"encoding/base64"
"encoding/binary"
2018-10-07 17:09:45 +00:00
"flag"
"fmt"
"io"
2018-11-07 21:16:13 +00:00
"math/rand"
2018-10-07 17:09:45 +00:00
"net"
"os"
"sync"
2019-08-02 00:01:19 +00:00
"sync/atomic"
2018-10-07 17:09:45 +00:00
"time"
"github.com/cbeuw/Cloak/internal/client"
mux "github.com/cbeuw/Cloak/internal/multiplex"
"github.com/cbeuw/Cloak/internal/util"
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 makeSession(sta *client.State) *mux.Session {
2019-08-02 14:45:33 +00:00
log.Info("Attemtping to start a new session")
2019-07-27 18:55:53 +00:00
if !sta.IsAdmin {
// sessionID is usergenerated. There shouldn't be a security concern because the scope of
// sessionID is limited to its UID.
quad := make([]byte, 4)
rand.Read(quad)
2019-08-02 00:01:19 +00:00
atomic.StoreUint32(&sta.SessionID, binary.BigEndian.Uint32(quad))
2019-07-31 23:43:33 +00:00
}
2019-08-06 14:50:33 +00:00
d := net.Dialer{Control: protector}
2019-08-02 00:01:19 +00:00
connsCh := make(chan net.Conn, sta.NumConn)
var _sessionKey atomic.Value
var wg sync.WaitGroup
for i := 0; i < sta.NumConn; i++ {
wg.Add(1)
go func() {
makeconn:
2019-08-06 14:50:33 +00:00
connectingIP := sta.RemoteHost
if net.ParseIP(connectingIP).To4() == nil {
// IPv6 needs square brackets
connectingIP = "[" + connectingIP + "]"
}
remoteConn, err := d.Dial("tcp", connectingIP+":"+sta.RemotePort)
if err != nil {
log.Errorf("Failed to establish new connections to remote: %v", err)
// TODO increase the interval if failed multiple times
time.Sleep(time.Second * 3)
goto makeconn
2019-08-06 14:50:33 +00:00
}
sk, err := client.PrepareConnection(sta, remoteConn)
if err != nil {
log.Errorf("Failed to prepare connection to remote: %v", err)
time.Sleep(time.Second * 3)
goto makeconn
}
_sessionKey.Store(sk)
2019-08-06 14:50:33 +00:00
connsCh <- remoteConn
wg.Done()
}()
}
wg.Wait()
2019-08-06 10:19:47 +00:00
log.Debug("All underlying connections established")
2019-08-02 00:01:19 +00:00
sessionKey := _sessionKey.Load().([]byte)
obfuscator, err := mux.GenerateObfs(sta.EncryptionMethod, sessionKey)
2019-08-02 00:01:19 +00:00
if err != nil {
log.Fatal(err)
}
2019-08-02 15:37:48 +00:00
sesh := mux.MakeSession(sta.SessionID, mux.UNLIMITED_VALVE, obfuscator, util.ReadTLS)
2019-08-02 00:01:19 +00:00
for i := 0; i < sta.NumConn; i++ {
conn := <-connsCh
sesh.AddConnection(conn)
}
2019-08-02 14:45:33 +00:00
log.Infof("Session %v established", sta.SessionID)
return sesh
}
2018-10-07 17:09:45 +00:00
func main() {
// 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
// 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
var config string
var b64AdminUID string
2018-11-22 21:55:23 +00:00
2018-10-07 17:09:45 +00:00
log_init()
2019-08-02 14:45:33 +00:00
log.SetLevel(log.DebugLevel)
2018-10-07 17:09:45 +00:00
if os.Getenv("SS_LOCAL_HOST") != "" {
localHost = os.Getenv("SS_LOCAL_HOST")
localPort = os.Getenv("SS_LOCAL_PORT")
remoteHost = os.Getenv("SS_REMOTE_HOST")
remotePort = os.Getenv("SS_REMOTE_PORT")
config = os.Getenv("SS_PLUGIN_OPTIONS")
2018-10-07 17:09:45 +00:00
} else {
localHost = "127.0.0.1"
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")
flag.StringVar(&config, "c", "ckclient.json", "config: path to the configuration file or options seperated with semicolons")
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")
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
}
2019-01-22 21:51:57 +00:00
sta := client.InitState(localHost, localPort, remoteHost, remotePort, time.Now)
err := sta.ParseConfig(config)
2018-10-07 17:09:45 +00:00
if err != nil {
log.Fatal(err)
}
if os.Getenv("SS_LOCAL_HOST") != "" {
sta.ProxyMethod = "shadowsocks"
}
if sta.LocalPort == "" {
2018-10-07 17:09:45 +00:00
log.Fatal("Must specify localPort")
}
if sta.RemoteHost == "" {
2018-10-07 17:09:45 +00:00
log.Fatal("Must specify remoteHost")
}
listeningIP := sta.LocalHost
2019-03-23 07:02:01 +00:00
if net.ParseIP(listeningIP).To4() == nil {
// IPv6 needs square brackets
listeningIP = "[" + listeningIP + "]"
}
listener, err := net.Listen("tcp", listeningIP+":"+sta.LocalPort)
2018-12-30 21:36:44 +00:00
if err != nil {
log.Fatal(err)
}
2018-10-07 17:09:45 +00:00
var adminUID []byte
if b64AdminUID != "" {
adminUID, err = base64.StdEncoding.DecodeString(b64AdminUID)
if err != nil {
log.Fatal(err)
}
}
if adminUID != nil {
2019-08-02 14:45:33 +00:00
log.Infof("API base is %v:%v", listeningIP, sta.LocalPort)
2019-07-27 18:55:53 +00:00
sta.IsAdmin = true
sta.SessionID = 0
sta.UID = adminUID
sta.NumConn = 1
2019-08-02 14:45:33 +00:00
} else {
log.Infof("Listening on %v:%v for proxy clients", listeningIP, sta.LocalPort)
}
var sesh *mux.Session
2019-01-21 21:29:18 +00:00
2018-10-07 17:09:45 +00:00
for {
localConn, err := listener.Accept()
2018-10-07 17:09:45 +00:00
if err != nil {
2019-08-02 14:45:33 +00:00
log.Error(err)
2018-10-07 17:09:45 +00:00
continue
}
2019-07-28 22:27:59 +00:00
if sesh == nil || sesh.IsClosed() {
sesh = makeSession(sta)
}
2018-10-07 17:09:45 +00:00
go func() {
2018-10-09 20:53:55 +00:00
data := make([]byte, 10240)
i, err := io.ReadAtLeast(localConn, data, 1)
2018-10-09 20:53:55 +00:00
if err != nil {
2019-08-02 14:45:33 +00:00
log.Errorf("Failed to read first packet from proxy client: %v", err)
localConn.Close()
2018-10-09 20:53:55 +00:00
return
}
2018-10-07 17:09:45 +00:00
stream, err := sesh.OpenStream()
if err != nil {
2019-08-02 14:45:33 +00:00
log.Errorf("Failed to open stream: %v", err)
localConn.Close()
2018-11-07 21:16:13 +00:00
return
}
_, err = stream.Write(data[:i])
if err != nil {
2019-08-02 14:45:33 +00:00
log.Errorf("Failed to write to stream: %v", err)
localConn.Close()
2018-12-09 23:45:06 +00:00
stream.Close()
return
2018-10-07 17:09:45 +00:00
}
2019-08-03 13:58:48 +00:00
go util.Pipe(localConn, stream)
util.Pipe(stream, localConn)
2018-10-07 17:09:45 +00:00
}()
}
}