2018-10-07 17:09:45 +00:00
|
|
|
// +build go1.11
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2018-11-07 21:16:13 +00:00
|
|
|
"math/rand"
|
2018-10-07 17:09:45 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
2018-11-08 19:47:53 +00:00
|
|
|
"sync"
|
2018-12-30 21:36:44 +00:00
|
|
|
"sync/atomic"
|
2018-10-07 17:09:45 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cbeuw/Cloak/internal/client"
|
|
|
|
"github.com/cbeuw/Cloak/internal/client/TLS"
|
|
|
|
mux "github.com/cbeuw/Cloak/internal/multiplex"
|
|
|
|
"github.com/cbeuw/Cloak/internal/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
var version string
|
|
|
|
|
|
|
|
func pipe(dst io.ReadWriteCloser, src io.ReadWriteCloser) {
|
2018-10-20 20:41:01 +00:00
|
|
|
// The maximum size of TLS message will be 16396+12. 12 because of the stream header
|
|
|
|
// 16408 is the max TLS message size on Firefox
|
|
|
|
buf := make([]byte, 16396)
|
2018-10-07 17:09:45 +00:00
|
|
|
for {
|
2018-10-20 10:35:50 +00:00
|
|
|
i, err := io.ReadAtLeast(src, buf, 1)
|
2018-10-09 15:07:54 +00:00
|
|
|
if err != nil || i == 0 {
|
2018-10-20 10:35:50 +00:00
|
|
|
go dst.Close()
|
|
|
|
go src.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i, err = dst.Write(buf[:i])
|
|
|
|
if err != nil || i == 0 {
|
2018-10-07 17:09:45 +00:00
|
|
|
go dst.Close()
|
|
|
|
go src.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This establishes a connection with ckserver and performs a handshake
|
2018-10-09 20:53:55 +00:00
|
|
|
func makeRemoteConn(sta *client.State) (net.Conn, error) {
|
2018-10-07 17:09:45 +00:00
|
|
|
|
2018-10-20 20:41:01 +00:00
|
|
|
// For android
|
2018-10-07 17:09:45 +00:00
|
|
|
d := net.Dialer{Control: protector}
|
|
|
|
|
|
|
|
clientHello := TLS.ComposeInitHandshake(sta)
|
|
|
|
remoteConn, err := d.Dial("tcp", sta.SS_REMOTE_HOST+":"+sta.SS_REMOTE_PORT)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Connecting to remote: %v\n", err)
|
2018-10-09 20:53:55 +00:00
|
|
|
return nil, err
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
_, err = remoteConn.Write(clientHello)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Sending ClientHello: %v\n", err)
|
2018-10-09 20:53:55 +00:00
|
|
|
return nil, err
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Three discarded messages: ServerHello, ChangeCipherSpec and Finished
|
|
|
|
discardBuf := make([]byte, 1024)
|
|
|
|
for c := 0; c < 3; c++ {
|
2018-11-07 21:16:13 +00:00
|
|
|
_, err = util.ReadTLS(remoteConn, discardBuf)
|
2018-10-07 17:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Reading discarded message %v: %v\n", c, err)
|
2018-10-09 20:53:55 +00:00
|
|
|
return nil, err
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := TLS.ComposeReply()
|
|
|
|
_, err = remoteConn.Write(reply)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Sending reply to remote: %v\n", err)
|
2018-10-09 20:53:55 +00:00
|
|
|
return nil, err
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 20:53:55 +00:00
|
|
|
return remoteConn, nil
|
2018-10-07 17:09:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Should be 127.0.0.1 to listen to ss-local on this machine
|
|
|
|
var localHost string
|
|
|
|
// server_port in ss config, ss sends data on loopback using this port
|
|
|
|
var localPort string
|
|
|
|
// The ip of the proxy server
|
|
|
|
var remoteHost string
|
|
|
|
// The proxy port,should be 443
|
|
|
|
var remotePort string
|
|
|
|
var pluginOpts string
|
2018-12-22 22:53:06 +00:00
|
|
|
isAdmin := new(bool)
|
2018-11-22 21:55:23 +00:00
|
|
|
|
2018-10-07 17:09:45 +00:00
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
|
|
|
|
log_init()
|
|
|
|
|
|
|
|
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")
|
|
|
|
pluginOpts = os.Getenv("SS_PLUGIN_OPTIONS")
|
|
|
|
} else {
|
|
|
|
localHost = "127.0.0.1"
|
|
|
|
flag.StringVar(&localPort, "l", "", "localPort: same as server_port in ss config, the plugin listens to SS using this")
|
|
|
|
flag.StringVar(&remoteHost, "s", "", "remoteHost: IP of your proxy server")
|
|
|
|
flag.StringVar(&remotePort, "p", "443", "remotePort: proxy port, should be 443")
|
|
|
|
flag.StringVar(&pluginOpts, "c", "ckclient.json", "pluginOpts: path to ckclient.json or options seperated with semicolons")
|
|
|
|
askVersion := flag.Bool("v", false, "Print the version number")
|
2018-12-11 23:26:05 +00:00
|
|
|
isAdmin = flag.Bool("a", false, "Admin mode")
|
2018-10-07 17:09:45 +00:00
|
|
|
printUsage := flag.Bool("h", false, "Print this message")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *askVersion {
|
|
|
|
fmt.Printf("ck-client %s\n", version)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if *printUsage {
|
|
|
|
flag.Usage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-30 01:15:46 +00:00
|
|
|
log.Println("Starting standalone mode")
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 01:15:46 +00:00
|
|
|
if *isAdmin {
|
|
|
|
sta := client.InitState("", "", "", "", time.Now, 0)
|
|
|
|
err := sta.ParseConfig(pluginOpts)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = adminPrompt(sta)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-12-30 21:36:44 +00:00
|
|
|
|
2018-11-07 21:16:13 +00:00
|
|
|
// sessionID is usergenerated. There shouldn't be a security concern because the scope of
|
|
|
|
// sessionID is limited to its UID.
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2018-12-30 01:15:46 +00:00
|
|
|
sessionID := rand.Uint32()
|
2018-11-07 21:16:13 +00:00
|
|
|
|
|
|
|
sta := client.InitState(localHost, localPort, remoteHost, remotePort, time.Now, sessionID)
|
2018-10-07 17:09:45 +00:00
|
|
|
err := sta.ParseConfig(pluginOpts)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sta.SS_LOCAL_PORT == "" {
|
|
|
|
log.Fatal("Must specify localPort")
|
|
|
|
}
|
|
|
|
if sta.SS_REMOTE_HOST == "" {
|
|
|
|
log.Fatal("Must specify remoteHost")
|
|
|
|
}
|
|
|
|
if sta.TicketTimeHint == 0 {
|
|
|
|
log.Fatal("TicketTimeHint cannot be empty or 0")
|
|
|
|
}
|
2018-12-30 21:36:44 +00:00
|
|
|
listener, err := net.Listen("tcp", sta.SS_LOCAL_HOST+":"+sta.SS_LOCAL_PORT)
|
|
|
|
log.Println("Listening for ss on " + sta.SS_LOCAL_HOST + ":" + sta.SS_LOCAL_PORT)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-10-07 17:09:45 +00:00
|
|
|
|
2018-12-30 21:36:44 +00:00
|
|
|
start:
|
2018-11-22 21:55:23 +00:00
|
|
|
var UNLIMITED int64 = 1e12
|
|
|
|
valve := mux.MakeValve(1e12, 1e12, &UNLIMITED, &UNLIMITED)
|
2018-12-09 23:45:06 +00:00
|
|
|
obfs := mux.MakeObfs(sta.UID)
|
|
|
|
deobfs := mux.MakeDeobfs(sta.UID)
|
2018-11-07 21:16:13 +00:00
|
|
|
sesh := mux.MakeSession(0, valve, obfs, deobfs, util.ReadTLS)
|
2018-10-07 17:09:45 +00:00
|
|
|
|
2018-11-08 19:47:53 +00:00
|
|
|
var wg sync.WaitGroup
|
2018-10-28 22:51:00 +00:00
|
|
|
for i := 0; i < sta.NumConn; i++ {
|
2018-11-08 19:47:53 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2018-12-30 21:36:44 +00:00
|
|
|
makeconn:
|
2018-11-08 19:47:53 +00:00
|
|
|
conn, err := makeRemoteConn(sta)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to establish new connections to remote: %v\n", err)
|
2018-12-30 21:36:44 +00:00
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
goto makeconn
|
2018-11-08 19:47:53 +00:00
|
|
|
}
|
|
|
|
sesh.AddConnection(conn)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
2018-11-08 19:47:53 +00:00
|
|
|
wg.Wait()
|
2018-10-07 17:09:45 +00:00
|
|
|
|
2018-12-30 21:36:44 +00:00
|
|
|
var broken uint32
|
2018-10-07 17:09:45 +00:00
|
|
|
for {
|
2018-12-30 21:36:44 +00:00
|
|
|
if atomic.LoadUint32(&broken) == 1 {
|
|
|
|
goto retry
|
|
|
|
}
|
2018-10-07 17:09:45 +00:00
|
|
|
ssConn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
go func() {
|
2018-10-09 20:53:55 +00:00
|
|
|
data := make([]byte, 10240)
|
|
|
|
i, err := io.ReadAtLeast(ssConn, data, 1)
|
|
|
|
if err != nil {
|
2018-12-09 23:45:06 +00:00
|
|
|
log.Println(err)
|
2018-10-09 20:53:55 +00:00
|
|
|
ssConn.Close()
|
|
|
|
return
|
|
|
|
}
|
2018-10-07 17:09:45 +00:00
|
|
|
stream, err := sesh.OpenStream()
|
|
|
|
if err != nil {
|
2018-12-30 21:36:44 +00:00
|
|
|
if err == mux.ErrBrokenSession {
|
|
|
|
atomic.StoreUint32(&broken, 1)
|
|
|
|
}
|
2018-12-09 23:45:06 +00:00
|
|
|
log.Println(err)
|
2018-10-07 17:09:45 +00:00
|
|
|
ssConn.Close()
|
2018-11-07 21:16:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = stream.Write(data[:i])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2018-12-09 23:45:06 +00:00
|
|
|
ssConn.Close()
|
|
|
|
stream.Close()
|
|
|
|
return
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
go pipe(ssConn, stream)
|
|
|
|
pipe(stream, ssConn)
|
|
|
|
}()
|
|
|
|
}
|
2018-12-30 21:36:44 +00:00
|
|
|
retry:
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
log.Println("Reconnecting")
|
|
|
|
goto start
|
2018-10-07 17:09:45 +00:00
|
|
|
|
|
|
|
}
|