Compare commits

...
18 Commits
Author SHA1 Message Date
xtaci 8705bd7670 upd 2016-08-27 09:17:16 +08:00
xtaci b4e5181a2f optimize retry strategy 2016-08-27 09:00:35 +08:00
xtaci 94f61b5945 upd param 2016-08-26 13:04:38 +08:00
xtaci efc0b99d21 Revert "replace yamux with muxado"
This reverts commit 3803993529.
2016-08-26 11:35:13 +08:00
xtaci 3803993529 replace yamux with muxado 2016-08-26 10:58:43 +08:00
xtaci d23ff7c351 upd 2016-08-25 20:34:33 +08:00
xtaci f383ee31a5 add autoexpire option 2016-08-25 20:33:04 +08:00
xtaci d399f220f0 remove a line 2016-08-25 20:04:34 +08:00
xtaci 59a1bbc7b8 SetFinalizer to session 2016-08-25 18:22:20 +08:00
xtaci 3b656e101f better api 2016-08-25 11:46:17 +08:00
xtaci aec364eb72 set tcp socket buffer 2016-08-24 12:27:33 +08:00
xtaci e26ab6729f spelling 2016-08-22 14:46:57 +08:00
xtaci 583fe3ba24 simple layer 2016-08-21 19:09:08 +08:00
xtaci 7fd6442284 refer to my fork 2016-08-20 21:15:05 +08:00
xtaci 81f4643830 Revert "refer to fork"
This reverts commit 744a6407e7.
2016-08-20 20:49:44 +08:00
xtaci 744a6407e7 refer to fork 2016-08-20 20:23:04 +08:00
xtaci 2cf5292ac1 revert yamux config 2016-08-20 18:37:52 +08:00
xtaci b3a8b631b4 upd README 2016-08-20 16:48:10 +08:00
5 changed files with 65 additions and 28 deletions
+1 -2
View File
@@ -3,7 +3,6 @@ MAINTAINER xtaci <daniel820313@gmail.com>
RUN apk update && \
apk upgrade && \
apk add git
RUN go get github.com/xtaci/kcptun/client
RUN go get github.com/xtaci/kcptun/server
RUN go get github.com/xtaci/kcptun/client && go get github.com/xtaci/kcptun/server
EXPOSE 29900/udp
EXPOSE 12948
+4 -4
View File
@@ -50,7 +50,7 @@ USAGE:
client_darwin_amd64 [global options] command [command options] [arguments...]
VERSION:
20160819
20160820
COMMANDS:
help, h Shows a list of commands or help for one command
@@ -59,7 +59,7 @@ GLOBAL OPTIONS:
--localaddr value, -l value local listen address (default: ":12948")
--remoteaddr value, -r value kcp server address (default: "vps:29900")
--key value pre-shared secret for client and server (default: "it's a secrect") [$KCPTUN_KEY]
--crypt value aes, aes-128, aes-192, blowfish, cast5, 3des, tea, xor, none (default: "aes")
--crypt value aes, aes-128, aes-192, salsa20, blowfish, twofish, cast5, 3des, tea, xtea, xor, none (default: "aes")
--mode value profiles: fast3, fast2, fast, normal (default: "fast")
--conn value set num of UDP connections to server (default: 1)
--mtu value set maximum transmission unit of UDP packets (default: 1350)
@@ -81,7 +81,7 @@ USAGE:
server_darwin_amd64 [global options] command [command options] [arguments...]
VERSION:
20160819
20160820
COMMANDS:
help, h Shows a list of commands or help for one command
@@ -90,7 +90,7 @@ GLOBAL OPTIONS:
--listen value, -l value kcp server listen address (default: ":29900")
--target value, -t value target server address (default: "127.0.0.1:12948")
--key value pre-shared secret for client and server (default: "it's a secrect") [$KCPTUN_KEY]
--crypt value aes, aes-128, aes-192, blowfish, cast5, 3des, tea, xor, none (default: "aes")
--crypt value aes, aes-128, aes-192, salsa20, blowfish, twofish, cast5, 3des, tea, xtea, xor, none (default: "aes")
--mode value profiles: fast3, fast2, fast, normal (default: "fast")
--mtu value set maximum transmission unit of UDP packets (default: 1350)
--sndwnd value set send window size(num of packets) (default: 1024)
+4 -4
View File
@@ -47,7 +47,7 @@ USAGE:
client_darwin_amd64 [global options] command [command options] [arguments...]
VERSION:
20160819
20160820
COMMANDS:
help, h Shows a list of commands or help for one command
@@ -56,7 +56,7 @@ GLOBAL OPTIONS:
--localaddr value, -l value local listen address (default: ":12948")
--remoteaddr value, -r value kcp server address (default: "vps:29900")
--key value pre-shared secret for client and server (default: "it's a secrect") [$KCPTUN_KEY]
--crypt value aes, aes-128, aes-192, blowfish, cast5, 3des, tea, xor, none (default: "aes")
--crypt value aes, aes-128, aes-192, salsa20, blowfish, twofish, cast5, 3des, tea, xtea, xor, none (default: "aes")
--mode value profiles: fast3, fast2, fast, normal (default: "fast")
--conn value set num of UDP connections to server (default: 1)
--mtu value set maximum transmission unit of UDP packets (default: 1350)
@@ -78,7 +78,7 @@ USAGE:
server_darwin_amd64 [global options] command [command options] [arguments...]
VERSION:
20160819
20160820
COMMANDS:
help, h Shows a list of commands or help for one command
@@ -87,7 +87,7 @@ GLOBAL OPTIONS:
--listen value, -l value kcp server listen address (default: ":29900")
--target value, -t value target server address (default: "127.0.0.1:12948")
--key value pre-shared secret for client and server (default: "it's a secrect") [$KCPTUN_KEY]
--crypt value aes, aes-128, aes-192, blowfish, cast5, 3des, tea, xor, none (default: "aes")
--crypt value aes, aes-128, aes-192, salsa20, blowfish, twofish, cast5, 3des, tea, xtea, xor, none (default: "aes")
--mode value profiles: fast3, fast2, fast, normal (default: "fast")
--mtu value set maximum transmission unit of UDP packets (default: 1350)
--sndwnd value set send window size(num of packets) (default: 1024)
+43 -13
View File
@@ -7,14 +7,15 @@ import (
"math/rand"
"net"
"os"
"runtime"
"time"
"golang.org/x/crypto/pbkdf2"
"github.com/golang/snappy"
"github.com/hashicorp/yamux"
"github.com/urfave/cli"
kcp "github.com/xtaci/kcp-go"
"github.com/xtaci/yamux"
)
var (
@@ -109,7 +110,7 @@ func main() {
cli.StringFlag{
Name: "key",
Value: "it's a secrect",
Usage: "pre-shared secret for client and server",
Usage: "pre-shared secret between client and server",
EnvVar: "KCPTUN_KEY",
},
cli.StringFlag{
@@ -127,10 +128,15 @@ func main() {
Value: 1,
Usage: "set num of UDP connections to server",
},
cli.IntFlag{
Name: "autoexpire",
Value: 0,
Usage: "set auto expiration time(in seconds) for a single UDP connection, 0 to disable",
},
cli.IntFlag{
Name: "mtu",
Value: 1350,
Usage: "set maximum transmission unit of UDP packets",
Usage: "set maximum transmission unit for UDP packets",
},
cli.IntFlag{
Name: "sndwnd",
@@ -254,6 +260,7 @@ func main() {
mtu, sndwnd, rcvwnd := c.Int("mtu"), c.Int("sndwnd"), c.Int("rcvwnd")
nocomp, acknodelay := c.Bool("nocomp"), c.Bool("acknodelay")
dscp, sockbuf, keepalive, conn := c.Int("dscp"), c.Int("sockbuf"), c.Int("keepalive"), c.Int("conn")
autoexpire := c.Int("autoexpire")
log.Println("listening on:", listener.Addr())
log.Println("encryption:", crypt)
@@ -268,6 +275,7 @@ func main() {
log.Println("sockbuf:", sockbuf)
log.Println("keepalive:", keepalive)
log.Println("conn:", conn)
log.Println("autoexpire:", autoexpire)
config := &yamux.Config{
AcceptBacklog: 256,
@@ -305,27 +313,49 @@ func main() {
session, err = yamux.Client(newCompStream(kcpconn), config)
}
checkError(err)
runtime.SetFinalizer(session, func(s *yamux.Session) {
s.Close()
})
return session
}
numconn := uint16(conn)
var muxes []*yamux.Session
for i := uint16(0); i < numconn; i++ {
muxes = append(muxes, createConn())
muxes := make([]struct {
session *yamux.Session
ttl time.Time
}, numconn)
for k := range muxes {
muxes[k].session = createConn()
muxes[k].ttl = time.Now().Add(time.Duration(autoexpire) * time.Second)
}
rr := uint16(0)
for {
p1, err := listener.AcceptTCP()
if err := p1.SetReadBuffer(sockbuf); err != nil {
log.Println("TCP SetReadBuffer:", err)
}
if err := p1.SetWriteBuffer(sockbuf); err != nil {
log.Println("TCP SetWriteBuffer:", err)
}
checkError(err)
mux := muxes[rr%numconn]
p2, err := mux.Open()
idx := rr % numconn
OPEN_P2:
// do auto expiration
if autoexpire > 0 && time.Now().After(muxes[idx].ttl) {
log.Println("autoexpired")
muxes[idx].session = createConn()
muxes[idx].ttl = time.Now().Add(time.Duration(autoexpire) * time.Second)
}
// do session open
p2, err := muxes[idx].session.Open()
if err != nil { // yamux failure
log.Println(err)
p1.Close()
muxes[rr%numconn] = createConn()
mux.Close()
continue
muxes[idx].session = createConn()
muxes[idx].ttl = time.Now().Add(time.Duration(autoexpire) * time.Second)
goto OPEN_P2
}
go handleClient(p1, p2)
rr++
+13 -5
View File
@@ -12,9 +12,9 @@ import (
"golang.org/x/crypto/pbkdf2"
"github.com/golang/snappy"
"github.com/hashicorp/yamux"
"github.com/urfave/cli"
kcp "github.com/xtaci/kcp-go"
"github.com/xtaci/yamux"
)
var (
@@ -61,18 +61,26 @@ func handleMux(conn io.ReadWriteCloser, target string, config *yamux.Config) {
return
}
defer mux.Close()
for {
p1, err := mux.Accept()
if err != nil {
log.Println(err)
return
}
sockbuf := int(config.MaxStreamWindowSize)
p2, err := net.DialTimeout("tcp", target, 5*time.Second)
if err != nil {
log.Println(err)
return
}
if err := p2.(*net.TCPConn).SetReadBuffer(sockbuf); err != nil {
log.Println("TCP SetReadBuffer:", err)
}
if err := p2.(*net.TCPConn).SetWriteBuffer(sockbuf); err != nil {
log.Println("TCP SetWriteBuffer:", err)
}
go handleClient(p1, p2)
}
}
@@ -127,7 +135,7 @@ func main() {
cli.StringFlag{
Name: "key",
Value: "it's a secrect",
Usage: "pre-shared secret for client and server",
Usage: "pre-shared secret between client and server",
EnvVar: "KCPTUN_KEY",
},
cli.StringFlag{
@@ -143,7 +151,7 @@ func main() {
cli.IntFlag{
Name: "mtu",
Value: 1350,
Usage: "set maximum transmission unit of UDP packets",
Usage: "set maximum transmission unit for UDP packets",
},
cli.IntFlag{
Name: "sndwnd",
@@ -296,7 +304,7 @@ func main() {
LogOutput: os.Stderr,
}
for {
if conn, err := lis.Accept(); err == nil {
if conn, err := lis.AcceptKCP(); err == nil {
log.Println("remote address:", conn.RemoteAddr())
conn.SetStreamMode(true)
conn.SetNoDelay(nodelay, interval, resend, nc)