Compare commits

..
51 Commits
Author SHA1 Message Date
xtaci a1fa76173a upgrade to kcp-go@v5.5.14 2020-07-01 18:17:12 +08:00
xtaci 912a97993e upgrade to smux@v1.5.14 2020-05-20 23:13:35 +08:00
TexotandGitHub 706274829f Add iptables to docker image (#786) 2020-04-24 09:48:13 +08:00
xtaci 4358f5600f update Dockerfile 2020-04-10 21:49:24 +08:00
xtaci 7cf99098cf upd to v5.5.11 2020-03-22 22:02:19 +08:00
xtaci 720050cc39 upd deps 2020-03-22 21:48:55 +08:00
xtaci e99088b8d2 upd go.mod 2020-03-21 14:11:03 +08:00
xtaci 4b574ecbc1 a bugfix in kcp-go, upgrade to kcp-go/v5@v5.5.9 2020-03-09 21:26:41 +08:00
xtaci 8115fe5db9 upd deps to kcp-go 2020-02-25 11:00:58 +08:00
xtaci 0dabb34467 update travis.yml 2020-02-24 10:58:17 +08:00
xtaci c290f0b008 upd deps 2020-02-23 12:02:09 +08:00
xtaci 01e7b9f4c2 upd deps to smux to@v1.5.12 2020-02-20 19:24:20 +08:00
xtaci 0da917322c upgrade to kcp-go@v5.5.6 2020-02-09 14:54:49 +00:00
xtaci 23cd29a88e update deps to kcp-go to v5.5.5 2020-02-01 17:26:23 +08:00
xtaciandGitHub 3c54e12586 Update README.md 2020-01-21 20:08:16 +08:00
xtaci c71120e7d4 upd deps 2020-01-16 22:27:05 +08:00
xtaci ee29dc4fb1 upgrade deps 2020-01-16 22:16:39 +08:00
xtaciandGitHub 77dc451e83 Update README.md 2020-01-05 11:26:55 +08:00
xtaci 8c2d39753e upgrade to kcp-go@v5.5.3 2020-01-05 00:03:29 +08:00
xtaci 3790c74ca8 upgrade to kcp-go@v5.5.2 2020-01-04 23:48:27 +08:00
xtaci b07931c582 upgrade to kcp-go@5.5.0 2020-01-04 17:04:55 +08:00
xtaci d374f93447 upgrade kcp-go@v5.4.26 2020-01-03 19:51:28 +08:00
xtaci c8fa2a287f update to kcp-go@v5.4.25 2020-01-03 16:04:07 +08:00
xtaci eefaf5afac upgrade to kcp-go/v5@v5.4.23 2020-01-03 15:51:06 +08:00
xtaciandGitHub 3b5b4ac729 Update README.md 2020-01-02 13:13:36 +08:00
xtaciandGitHub b983528f33 Update README.md 2020-01-01 12:16:55 +08:00
xtaci 0f4131b7ae use go.mod based kcp-go 2019-12-31 22:45:36 +08:00
xtaci 0c315300a3 add an example systemd service 2019-12-31 15:09:22 +08:00
xtaci 7490778bbb upgrade to smux@v1.5.7 2019-12-29 18:39:24 +08:00
xtaci fc57c98098 rawcopy leads mistakes in smux in v2 2019-12-29 18:31:26 +08:00
xtaci 34bc48fd64 Squashed commit of the following:
commit 58d2c9d3468aa1c04f8cb60f0680186b6330804c
Author: xtaci <daniel820313@gmail.com>
Date:   Thu Dec 26 17:40:36 2019 +0800

    combine smux v1 & v2
2019-12-26 17:45:07 +08:00
xtaciandGitHub 56b45e84f7 Add files via upload 2019-12-20 10:26:12 +08:00
xtaci 98f93118cf smux@v1.4.8 & smux/v2@v2.0.18 2019-12-19 16:42:46 +08:00
xtaci dca5b2da39 upgrade deps to smux 2019-12-19 16:25:37 +08:00
xtaciandGitHub 5bd9b7e92b Update README.md 2019-12-19 15:15:46 +08:00
xtaci f3cccbb51c fix closing scheme 2019-12-19 14:52:45 +08:00
xtaci 4f88883837 fix termination sequence 2019-12-19 14:44:27 +08:00
xtaci 8a03eb1568 eliminate extra 1 goroutine per connection 2019-12-19 14:12:44 +08:00
xtaci 9e0d6f8ce8 upgrade to kcp-go@v5.4.20 2019-12-19 11:23:18 +08:00
xtaci de73ed90a5 add comments to rawCopy 2019-12-18 21:23:00 +08:00
xtaci 90ea4baa06 fallback on windows 2019-12-18 19:59:54 +08:00
xtaci c8c084a087 adjusts to 4k buf per mux 2019-12-18 16:42:24 +08:00
xtaci 5ad2b958a6 fix locking scheme 2019-12-18 16:26:46 +08:00
xtaci 361ba7600d fix error 2019-12-18 15:50:09 +08:00
xtaci 61e830c4e8 write controlled reading based on RawConn to buffer per stream 2019-12-18 15:42:48 +08:00
xtaci 216c7aaf6d upgrade smux to v1.4.6 & v2.0.16 2019-11-26 23:01:47 +08:00
xtaci 876e17ab18 upgrade to smux/v2@v2.0.15 2019-11-26 22:52:27 +08:00
xtaci 166dbca282 upgrade to github.com/xtaci/smux@v1.4.5 github.com/xtaci/smux/v2@v2.0.14, optimize Copy to use io.WriteTo 2019-11-26 22:18:57 +08:00
xtaci 24ff7caecb shrink per-direction buffer to 4k 2019-11-12 15:07:45 +08:00
xtaci 99abd12302 upgrade to smux@v2.0.13 2019-11-12 14:27:57 +08:00
xtaci 4a49552392 force GO111MODULE to on while compiling 2019-11-10 11:27:22 +08:00
16 changed files with 195 additions and 281 deletions
-2
View File
@@ -1,7 +1,5 @@
language: go
go:
- 1.11.x
- 1.12.x
- 1.13.x
before_install:
- go get github.com/mattn/goveralls
+3 -2
View File
@@ -1,4 +1,4 @@
FROM golang:alpine as builder
FROM golang:1.14.2-alpine3.11 as builder
MAINTAINER xtaci <daniel820313@gmail.com>
ENV GO111MODULE=on
RUN apk update && \
@@ -6,7 +6,8 @@ RUN apk update && \
apk add git gcc libc-dev linux-headers
RUN go get -ldflags "-X main.VERSION=$(date -u +%Y%m%d) -s -w" github.com/xtaci/kcptun/client && go get -ldflags "-X main.VERSION=$(date -u +%Y%m%d) -s -w" github.com/xtaci/kcptun/server
FROM alpine:3.9
FROM alpine:3.11
RUN apk add --no-cache iptables
COPY --from=builder /go/bin /bin
EXPOSE 29900/udp
EXPOSE 12948
+20 -11
View File
@@ -1,20 +1,18 @@
# <img src="logo.png" alt="kcptun" height="54px" />
[![Release][13]][14] [![Powered][17]][18] [![MIT licensed][11]][12] [![Build Status][3]][4] [![Go Report Card][5]][6] [![Downloads][15]][16] [![Docker][1]][2]
[![Release][13]][14] [![Powered][17]][18] [![MIT licensed][11]][12] [![Build Status][3]][4] [![Go Report Card][5]][6] [![Downloads][15]][16] [![Docker][1]][2]
[1]: https://images.microbadger.com/badges/image/xtaci/kcptun.svg
[2]: https://microbadger.com/images/xtaci/kcptun
[1]: https://img.shields.io/docker/pulls/xtaci/kcptun
[2]: https://hub.docker.com/r/xtaci/kcptun
[3]: https://travis-ci.org/xtaci/kcptun.svg?branch=master
[4]: https://travis-ci.org/xtaci/kcptun
[5]: https://goreportcard.com/badge/github.com/xtaci/kcptun
[6]: https://goreportcard.com/report/github.com/xtaci/kcptun
[7]: https://img.shields.io/badge/license-MIT-blue.svg
[8]: https://raw.githubusercontent.com/xtaci/kcptun/master/LICENSE.md
[11]: https://img.shields.io/badge/license-MIT-blue.svg
[11]: https://img.shields.io/github/license/xtaci/kcptun
[12]: LICENSE.md
[13]: https://img.shields.io/github/release/xtaci/kcptun.svg
[13]: https://img.shields.io/github/v/release/xtaci/kcptun?color=orange
[14]: https://github.com/xtaci/kcptun/releases/latest
[15]: https://img.shields.io/github/downloads/xtaci/kcptun/total.svg?maxAge=1800
[15]: https://img.shields.io/github/downloads/xtaci/kcptun/total.svg?maxAge=1800&color=orange
[16]: https://github.com/xtaci/kcptun/releases
[17]: https://img.shields.io/badge/KCP-Powered-blue.svg
[18]: https://github.com/skywind3000/kcp
@@ -23,6 +21,15 @@
> *Disclaimer: kcptun maintains a single website — [github.com/xtaci/kcptun](https://github.com/xtaci/kcptun). Any websites other than [github.com/xtaci/kcptun](https://github.com/xtaci/kcptun) are not endorsed by xtaci.*
### Requirements
| Target | Minimum | Recommended |
| --- | --- | --- |
| System | aix darwin dragonfly freebsd linux netbsd openbsd solaris windows | linux |
| Memory | >20MB | >32MB |
| CPU | ANY | amd64 with AES-NI & AVX2 |
### QuickStart
Increase the number of open files on your server, as:
@@ -59,11 +66,13 @@ which tunnels the original connection:
> Application -> Target Server(8388/tcp)
### Install from source
### Build from source
```
$ export GO111MODULE=on
$ go get -u github.com/xtaci/kcptun/...
$ git clone https://github.com/xtaci/kcptun.git
$ cd kcptun
$ ./build-release.sh
$ cd build
```
All precompiled releases are genereated from `build-release.sh` script.
+2 -5
View File
@@ -6,11 +6,8 @@ cd $BUILD_DIR
sum="sha1sum"
if [ "$GO111MODULE" != "on" ]; then
echo "GO111MODULE is off"
else
echo "GO111MODULE is on"
fi
export GO111MODULE=on
echo "Setting GO111MODULE to" $GO111MODULE
if ! hash sha1sum 2>/dev/null; then
if ! hash shasum 2>/dev/null; then
+1 -1
View File
@@ -2,7 +2,7 @@ package main
import (
"github.com/pkg/errors"
kcp "github.com/xtaci/kcp-go"
kcp "github.com/xtaci/kcp-go/v5"
"github.com/xtaci/tcpraw"
)
+49 -88
View File
@@ -8,39 +8,38 @@ import (
"math/rand"
"net"
"os"
"sync"
"time"
"golang.org/x/crypto/pbkdf2"
"github.com/pkg/errors"
"github.com/urfave/cli"
kcp "github.com/xtaci/kcp-go"
kcp "github.com/xtaci/kcp-go/v5"
"github.com/xtaci/kcptun/generic"
"github.com/xtaci/smux"
smuxv2 "github.com/xtaci/smux/v2"
)
// SALT is use for pbkdf2 key expansion
const SALT = "kcp-go"
// maximum supported smux version
const maxSmuxVer = 2
const (
// SALT is use for pbkdf2 key expansion
SALT = "kcp-go"
// maximum supported smux version
maxSmuxVer = 2
// stream copy buffer size
bufSize = 4096
)
// VERSION is injected by buildflags
var VERSION = "SELFBUILD"
// A pool for stream copying
var xmitBuf sync.Pool
func handleClient(mux generic.Mux, p1 net.Conn, quiet bool) {
// handleClient aggregates connection p1 on mux with 'writeLock'
func handleClient(session *smux.Session, p1 net.Conn, quiet bool) {
logln := func(v ...interface{}) {
if !quiet {
log.Println(v...)
}
}
defer p1.Close()
p2, err := mux.Open()
p2, err := session.OpenStream()
if err != nil {
logln(err)
return
@@ -48,42 +47,23 @@ func handleClient(mux generic.Mux, p1 net.Conn, quiet bool) {
defer p2.Close()
if s2, ok := p2.(generic.Stream); ok {
logln("stream opened", "in:", p1.RemoteAddr(), "out:", fmt.Sprint(s2.RemoteAddr(), "(", s2.ID(), ")"))
defer logln("stream closed", "in:", p1.RemoteAddr(), "out:", fmt.Sprint(s2.RemoteAddr(), "(", s2.ID(), ")"))
}
logln("stream opened", "in:", p1.RemoteAddr(), "out:", fmt.Sprint(p2.RemoteAddr(), "(", p2.ID(), ")"))
defer logln("stream closed", "in:", p1.RemoteAddr(), "out:", fmt.Sprint(p2.RemoteAddr(), "(", p2.ID(), ")"))
// start tunnel & wait for tunnel termination
streamCopy := func(dst io.Writer, src io.ReadCloser) chan struct{} {
die := make(chan struct{})
go func() {
buf := xmitBuf.Get().([]byte)
if _, err := generic.CopyBuffer(dst, src, buf); err != nil {
if s2, ok := p2.(generic.Stream); ok {
// verbose error handling
cause := err
if e, ok := err.(interface{ Cause() error }); ok {
cause = e.Cause()
}
switch cause {
case smux.ErrInvalidProtocol:
log.Println("smux version:1", err, "in:", p1.RemoteAddr(), "out:", fmt.Sprint(s2.RemoteAddr(), "(", s2.ID(), ")"))
case smuxv2.ErrInvalidProtocol:
log.Println("smux version:2", err, "in:", p1.RemoteAddr(), "out:", fmt.Sprint(s2.RemoteAddr(), "(", s2.ID(), ")"))
}
}
streamCopy := func(dst io.Writer, src io.ReadCloser) {
if _, err := generic.Copy(dst, src); err != nil {
// report protocol error
if err == smux.ErrInvalidProtocol {
log.Println("smux", err, "in:", p1.RemoteAddr(), "out:", fmt.Sprint(p2.RemoteAddr(), "(", p2.ID(), ")"))
}
xmitBuf.Put(buf)
close(die)
}()
return die
}
p1.Close()
p2.Close()
}
select {
case <-streamCopy(p1, p2):
case <-streamCopy(p2, p1):
}
go streamCopy(p1, p2)
streamCopy(p2, p1)
}
func checkError(err error) {
@@ -99,9 +79,6 @@ func main() {
// add more log flags for debugging
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
xmitBuf.New = func() interface{} {
return make([]byte, 32768)
}
myApp := cli.NewApp()
myApp.Name = "kcptun"
@@ -387,7 +364,7 @@ func main() {
block, _ = kcp.NewAESBlockCrypt(pass)
}
createConn := func() (generic.Mux, error) {
createConn := func() (*smux.Session, error) {
kcpconn, err := dial(&config, block)
if err != nil {
return nil, errors.Wrap(err, "dial()")
@@ -409,47 +386,31 @@ func main() {
log.Println("SetWriteBuffer:", err)
}
log.Println("smux version:", config.SmuxVer, "on connection:", kcpconn.LocalAddr(), "->", kcpconn.RemoteAddr())
switch config.SmuxVer {
case 1:
smuxConfig := smux.DefaultConfig()
smuxConfig.MaxReceiveBuffer = config.SmuxBuf
smuxConfig.KeepAliveInterval = time.Duration(config.KeepAlive) * time.Second
smuxConfig := smux.DefaultConfig()
smuxConfig.Version = config.SmuxVer
smuxConfig.MaxReceiveBuffer = config.SmuxBuf
smuxConfig.MaxStreamBuffer = config.StreamBuf
smuxConfig.KeepAliveInterval = time.Duration(config.KeepAlive) * time.Second
// stream multiplex
var session *smux.Session
if config.NoComp {
session, err = smux.Client(kcpconn, smuxConfig)
} else {
session, err = smux.Client(generic.NewCompStream(kcpconn), smuxConfig)
}
if err != nil {
return nil, errors.Wrap(err, "createConn()")
}
return session, nil
case 2:
smuxConfig := smuxv2.DefaultConfig()
smuxConfig.MaxReceiveBuffer = config.SmuxBuf
smuxConfig.MaxStreamBuffer = config.StreamBuf
smuxConfig.KeepAliveInterval = time.Duration(config.KeepAlive) * time.Second
// stream multiplex
var session *smuxv2.Session
if config.NoComp {
session, err = smuxv2.Client(kcpconn, smuxConfig)
} else {
session, err = smuxv2.Client(generic.NewCompStream(kcpconn), smuxConfig)
}
if err != nil {
return nil, errors.Wrap(err, "createConn()")
}
return session, nil
default:
panic("incorrect smux version")
if err := smux.VerifyConfig(smuxConfig); err != nil {
log.Fatalf("%+v", err)
}
// stream multiplex
var session *smux.Session
if config.NoComp {
session, err = smux.Client(kcpconn, smuxConfig)
} else {
session, err = smux.Client(generic.NewCompStream(kcpconn), smuxConfig)
}
if err != nil {
return nil, errors.Wrap(err, "createConn()")
}
return session, nil
}
// wait until a connection is ready
waitConn := func() generic.Mux {
waitConn := func() *smux.Session {
for {
if session, err := createConn(); err == nil {
return session
@@ -462,7 +423,7 @@ func main() {
numconn := uint16(config.Conn)
muxes := make([]struct {
session generic.Mux
session *smux.Session
ttl time.Time
}, numconn)
@@ -471,7 +432,7 @@ func main() {
muxes[k].ttl = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
}
chScavenger := make(chan generic.Mux, 128)
chScavenger := make(chan *smux.Session, 128)
go scavenger(chScavenger, config.ScavengeTTL)
go generic.SnmpLogger(config.SnmpLog, config.SnmpPeriod)
rr := uint16(0)
@@ -497,11 +458,11 @@ func main() {
}
type scavengeSession struct {
session generic.Mux
session *smux.Session
ts time.Time
}
func scavenger(ch chan generic.Mux, ttl int) {
func scavenger(ch chan *smux.Session, ttl int) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
var sessionList []scavengeSession
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"os/signal"
"syscall"
kcp "github.com/xtaci/kcp-go"
kcp "github.com/xtaci/kcp-go/v5"
)
func init() {
+16
View File
@@ -0,0 +1,16 @@
Description=kcptun
Wants=network.target
After=syslog.target network-online.target
[Service]
Type=simple
Environment=GOGC=20
ExecStart=/home/user/client_linux_amd64 -c /home/user/local.json
Restart=on-failure
RestartSec=10
KillMode=process
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
+18 -30
View File
@@ -1,36 +1,24 @@
package generic
import "io"
import (
"io"
)
// io.CopyBuffer has extra tests for interface like io.ReaderFrom and io.WriterTo
// which is not efficient in memory management from tests
func CopyBuffer(dst io.Writer, src io.Reader, buf []byte) (written int64, err error) {
if buf != nil && len(buf) == 0 {
panic("empty buffer in copyBuffer")
const bufSize = 4096
// Memory optimized io.Copy function specified for this library
func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
// If the reader has a WriteTo method, use it to do the copy.
// Avoids an allocation and a copy.
if wt, ok := src.(io.WriterTo); ok {
return wt.WriteTo(dst)
}
// Similarly, if the writer has a ReadFrom method, use it to do the copy.
if rt, ok := dst.(io.ReaderFrom); ok {
return rt.ReadFrom(src)
}
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return written, err
// fallback to standard io.CopyBuffer
buf := make([]byte, bufSize)
return io.CopyBuffer(dst, src, buf)
}
-21
View File
@@ -1,21 +0,0 @@
package generic
import (
"io"
"net"
)
type Mux interface {
Open() (io.ReadWriteCloser, error)
Accept() (io.ReadWriteCloser, error)
IsClosed() bool
NumStreams() int
RemoteAddr() net.Addr
Close() error
}
type Stream interface {
io.ReadWriteCloser
ID() uint32
RemoteAddr() net.Addr
}
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"path/filepath"
"time"
"github.com/xtaci/kcp-go"
kcp "github.com/xtaci/kcp-go/v5"
)
func SnmpLogger(path string, interval int) {
+11 -13
View File
@@ -4,21 +4,19 @@ require (
github.com/coreos/go-iptables v0.4.2 // indirect
github.com/golang/snappy v0.0.1
github.com/google/gopacket v1.1.17 // indirect
github.com/klauspost/cpuid v1.2.1 // indirect
github.com/klauspost/reedsolomon v1.9.2 // indirect
github.com/pkg/errors v0.8.1
github.com/klauspost/cpuid v1.2.3 // indirect
github.com/pkg/errors v0.9.1
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b // indirect
github.com/tjfoc/gmsm v1.0.1 // indirect
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b // indirect
github.com/tjfoc/gmsm v1.3.0 // indirect
github.com/urfave/cli v1.21.0
github.com/xtaci/kcp-go v5.4.19+incompatible
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae // indirect
github.com/xtaci/smux v1.4.4
github.com/xtaci/smux/v2 v2.0.11
github.com/xtaci/kcp-go v5.4.20+incompatible // indirect
github.com/xtaci/kcp-go/v5 v5.5.14
github.com/xtaci/smux v1.5.14
github.com/xtaci/tcpraw v1.2.25
golang.org/x/crypto v0.0.0-20190909091759-094676da4a83
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b // indirect
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b // indirect
golang.org/x/crypto v0.0.0-20200320181102-891825fb96df
golang.org/x/net v0.0.0-20200320220750-118fecf932d8 // indirect
golang.org/x/sys v0.0.0-20200321134203-328b4cd54aae // indirect
)
go 1.13
go 1.14
+39 -30
View File
@@ -5,53 +5,62 @@ github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/gopacket v1.1.17 h1:rMrlX2ZY2UbvT+sdz3+6J+pp2z+msCq9MxTU6ymxbBY=
github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM=
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/reedsolomon v1.9.2 h1:E9CMS2Pqbv+C7tsrYad4YC9MfhnMVWhMRsTi7U0UB18=
github.com/klauspost/reedsolomon v1.9.2/go.mod h1:CwCi+NUr9pqSVktrkN+Ondf06rkhYZ/pcNv7fu+8Un4=
github.com/klauspost/cpuid v1.2.2 h1:1xAgYebNnsb9LKCdLOvFWtAxGU/33mjJtyOVbmUa0Us=
github.com/klauspost/cpuid v1.2.2/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.2.3 h1:CCtW0xUnWGVINKvE/WWOYKdsPV6mawAtvQuSl8guwQs=
github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/reedsolomon v1.9.3 h1:N/VzgeMfHmLc+KHMD1UL/tNkfXAt8FnUqlgXGIduwAY=
github.com/klauspost/reedsolomon v1.9.3/go.mod h1:CwCi+NUr9pqSVktrkN+Ondf06rkhYZ/pcNv7fu+8Un4=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/templexxx/cpu v0.0.1 h1:hY4WdLOgKdc8y13EYklu9OUTXik80BkxHoWvTO6MQQY=
github.com/templexxx/cpu v0.0.1/go.mod h1:w7Tb+7qgcAlIyX4NhLuDKt78AHA5SzPmq0Wj6HiEnnk=
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 h1:89CEmDvlq/F7SJEOqkIdNDGJXrQIhuIx9D2DBXjavSU=
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161/go.mod h1:wM7WEvslTq+iOEAMDLSzhVuOt5BRZ05WirO+b09GHQU=
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b h1:mnG1fcsIB1d/3vbkBak2MM0u+vhGhlQwpeimUi7QncM=
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b h1:fj5tQ8acgNUr6O8LEplsxDhUIe2573iLkJc+PqnzZTI=
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
github.com/templexxx/xorsimd v0.4.1 h1:iUZcywbOYDRAZUasAs2eSCUW8eobuZDy0I9FJiORkVg=
github.com/templexxx/xorsimd v0.4.1/go.mod h1:W+ffZz8jJMH2SXwuKu9WhygqBMbFnp14G2fqEr8qaNo=
github.com/tjfoc/gmsm v1.0.1 h1:R11HlqhXkDospckjZEihx9SW/2VW0RgdwrykyWMFOQU=
github.com/tjfoc/gmsm v1.0.1/go.mod h1:XxO4hdhhrzAd+G4CjDqaOkd0hUzmtPR/d3EiBBMn/wc=
github.com/tjfoc/gmsm v1.3.0 h1:i7c6Za/IlgBvnGxYpfD7L3TGuaS+v6oGcgq+J9/ecEA=
github.com/tjfoc/gmsm v1.3.0/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE=
github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ=
github.com/xtaci/kcp-go v5.4.13+incompatible h1:s6ba2XTw8lAj+s6AQNob25dCvWDgwE+U1QpEVBUoYy8=
github.com/xtaci/kcp-go v5.4.13+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/kcp-go v5.4.14+incompatible h1:kQZr/ngKQtYrgXSUxwF4A59mTMzUp0BDmtWIRuXYoqg=
github.com/xtaci/kcp-go v5.4.14+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/kcp-go v5.4.15+incompatible h1:QLDulPaKjT4k4cGeviyC1mt00gwJ3r5epx8yCw6ACEc=
github.com/xtaci/kcp-go v5.4.15+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/kcp-go v5.4.16+incompatible h1:/L7UP4P4H/oXpMnrb2W9oOxCMVpjPi3FJeMJmgN+SUE=
github.com/xtaci/kcp-go v5.4.16+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/kcp-go v5.4.17+incompatible h1:RudP76JCx062JSxPxSjBl+457+fS0M7T8zEZPLpC0o8=
github.com/xtaci/kcp-go v5.4.17+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/kcp-go v5.4.18+incompatible h1:zxzRP8V54vhJ8QAKEjf1b9g96R01prybCRchx6rEmtg=
github.com/xtaci/kcp-go v5.4.18+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/kcp-go v5.4.19+incompatible h1:vv7Ar1D9WZGiv6deIOluxrC26Oin/2jFtx8sFU5tlvw=
github.com/xtaci/kcp-go v5.4.19+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/kcp-go v5.4.20+incompatible h1:TN1uey3Raw0sTz0Fg8GkfM0uH3YwzhnZWQ1bABv5xAg=
github.com/xtaci/kcp-go v5.4.20+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/kcp-go/v5 v5.5.11 h1:wYvoREAp6bG3sYwtxhY1QelbB/1fNggCtkt2DwqFj2M=
github.com/xtaci/kcp-go/v5 v5.5.11/go.mod h1:Oyw+zrBrO58urX1AaWV+2RynthEKcs+qrRAh0Q8YpdU=
github.com/xtaci/kcp-go/v5 v5.5.14-0.20200701100455-43d2c47d8ac1 h1:bnQj12vKzlMDxNGzFTj7ZDgXXpUpEQsX9mF/s7hD68o=
github.com/xtaci/kcp-go/v5 v5.5.14-0.20200701100455-43d2c47d8ac1/go.mod h1:H0T/EJ+lPNytnFYsKLH0JHUtiwZjG3KXlTM6c+Q4YUo=
github.com/xtaci/kcp-go/v5 v5.5.14 h1:YODIwvTyZmOTj3SduJeIcQPxthDoHllMm8YIBlK44Ik=
github.com/xtaci/kcp-go/v5 v5.5.14/go.mod h1:H0T/EJ+lPNytnFYsKLH0JHUtiwZjG3KXlTM6c+Q4YUo=
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae h1:J0GxkO96kL4WF+AIT3M4mfUVinOCPgf2uUWYFUzN0sM=
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae/go.mod h1:gXtu8J62kEgmN++bm9BVICuT/e8yiLI2KFobd/TRFsE=
github.com/xtaci/smux v1.4.4 h1:FukIfahko+KHhS9Gxppkp6756opZymvPOLNmpny1is4=
github.com/xtaci/smux v1.4.4/go.mod h1:LuA3S0xssf4fmGRJ7ow3EehgmDUzib4EcobaFNKvlMA=
github.com/xtaci/smux/v2 v2.0.11 h1:thVWmgGRciZ8iaATwpY2B/51aHzmMI6wrF7DfcJSckU=
github.com/xtaci/smux/v2 v2.0.11/go.mod h1:Iqy5a3Gax2p7WCKHOHkSNo/COthNFXd3/vqrcKNtzqI=
github.com/xtaci/smux v1.5.14 h1:1j+zJYDZRv9FHaWqCJfH5RPizIm0fSzJIFbfVn8zsfg=
github.com/xtaci/smux v1.5.14/go.mod h1:OMlQbT5vcgl2gb49mFkYo6SMf+zP3rcjcwQz7ZU7IGY=
github.com/xtaci/tcpraw v1.2.25 h1:VDlqo0op17JeXBM6e2G9ocCNLOJcw9mZbobMbJjo0vk=
github.com/xtaci/tcpraw v1.2.25/go.mod h1:dKyZ2V75s0cZ7cbgJYdxPvms7af0joIeOyx1GgJQbLk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190909091759-094676da4a83 h1:mgAKeshyNqWKdENOnQsg+8dRTwZFIwFaO3HNl52sweA=
golang.org/x/crypto v0.0.0-20190909091759-094676da4a83/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 h1:ULYEB3JvPRE/IfO+9uO7vKV/xzVTO7XPAwm8xbf4w2g=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200320181102-891825fb96df h1:lDWgvUvNnaTnNBc/dwOty86cFeKoKWbwy2wQj0gIxbU=
golang.org/x/crypto v0.0.0-20200320181102-891825fb96df/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b h1:XfVGCX+0T4WOStkaOsJRllbsiImhB2jgVBGc9L0lPGc=
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200320220750-118fecf932d8 h1:1+zQlQqEEhUeStBTi653GZAnAuivZq/2hz+Iz+OP7rg=
golang.org/x/net v0.0.0-20200320220750-118fecf932d8/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b h1:3S2h5FadpNr0zUUCVZjlKIEYF+KaX/OBplTGo89CYHI=
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8 h1:JA8d3MPx/IToSyXZG/RhwYEtfrKO1Fxrqe8KrkiLXKM=
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200321134203-328b4cd54aae h1:3tcmuaB7wwSZtelmiv479UjUB+vviwABz7a133ZwOKQ=
golang.org/x/sys v0.0.0-20200321134203-328b4cd54aae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 55 KiB

+33 -75
View File
@@ -16,25 +16,24 @@ import (
"golang.org/x/crypto/pbkdf2"
"github.com/urfave/cli"
kcp "github.com/xtaci/kcp-go"
kcp "github.com/xtaci/kcp-go/v5"
"github.com/xtaci/kcptun/generic"
"github.com/xtaci/smux"
smuxv2 "github.com/xtaci/smux/v2"
"github.com/xtaci/tcpraw"
)
// SALT is use for pbkdf2 key expansion
const SALT = "kcp-go"
// maximum supported smux version
const maxSmuxVer = 2
const (
// SALT is use for pbkdf2 key expansion
SALT = "kcp-go"
// maximum supported smux version
maxSmuxVer = 2
// stream copy buffer size
bufSize = 4096
)
// VERSION is injected by buildflags
var VERSION = "SELFBUILD"
// A pool for stream copying
var xmitBuf sync.Pool
// handle multiplex-ed connection
func handleMux(conn net.Conn, config *Config) {
// check if target is unix domain socket
@@ -45,45 +44,27 @@ func handleMux(conn net.Conn, config *Config) {
log.Println("smux version:", config.SmuxVer, "on connection:", conn.LocalAddr(), "->", conn.RemoteAddr())
// stream multiplex
var muxer generic.Mux
switch config.SmuxVer {
case 1:
smuxConfig := smux.DefaultConfig()
smuxConfig.MaxReceiveBuffer = config.SmuxBuf
smuxConfig.KeepAliveInterval = time.Duration(config.KeepAlive) * time.Second
smuxConfig := smux.DefaultConfig()
smuxConfig.Version = config.SmuxVer
smuxConfig.MaxReceiveBuffer = config.SmuxBuf
smuxConfig.MaxStreamBuffer = config.StreamBuf
smuxConfig.KeepAliveInterval = time.Duration(config.KeepAlive) * time.Second
mux, err := smux.Server(conn, smuxConfig)
if err != nil {
log.Println(err)
return
}
defer mux.Close()
muxer = mux
case 2:
smuxConfig := smuxv2.DefaultConfig()
smuxConfig.MaxReceiveBuffer = config.SmuxBuf
smuxConfig.MaxStreamBuffer = config.StreamBuf
smuxConfig.KeepAliveInterval = time.Duration(config.KeepAlive) * time.Second
mux, err := smuxv2.Server(conn, smuxConfig)
if err != nil {
log.Println(err)
return
}
defer mux.Close()
muxer = mux
default:
panic("incorrect smux version")
mux, err := smux.Server(conn, smuxConfig)
if err != nil {
log.Println(err)
return
}
defer mux.Close()
for {
stream, err := muxer.Accept()
stream, err := mux.AcceptStream()
if err != nil {
log.Println(err)
return
}
go func(p1 io.ReadWriteCloser) {
go func(p1 *smux.Stream) {
var p2 net.Conn
var err error
if !isUnix {
@@ -102,7 +83,7 @@ func handleMux(conn net.Conn, config *Config) {
}
}
func handleClient(p1 io.ReadWriteCloser, p2 net.Conn, quiet bool) {
func handleClient(p1 *smux.Stream, p2 net.Conn, quiet bool) {
logln := func(v ...interface{}) {
if !quiet {
log.Println(v...)
@@ -112,42 +93,22 @@ func handleClient(p1 io.ReadWriteCloser, p2 net.Conn, quiet bool) {
defer p1.Close()
defer p2.Close()
if s1, ok := p1.(generic.Stream); ok {
logln("stream opened", "in:", fmt.Sprint(s1.RemoteAddr(), "(", s1.ID(), ")"), "out:", p2.RemoteAddr())
defer logln("stream closed", "in:", fmt.Sprint(s1.RemoteAddr(), "(", s1.ID(), ")"), "out:", p2.RemoteAddr())
}
logln("stream opened", "in:", fmt.Sprint(p1.RemoteAddr(), "(", p1.ID(), ")"), "out:", p2.RemoteAddr())
defer logln("stream closed", "in:", fmt.Sprint(p1.RemoteAddr(), "(", p1.ID(), ")"), "out:", p2.RemoteAddr())
// start tunnel & wait for tunnel termination
streamCopy := func(dst io.Writer, src io.ReadCloser) chan struct{} {
die := make(chan struct{})
go func() {
buf := xmitBuf.Get().([]byte)
if _, err := generic.CopyBuffer(dst, src, buf); err != nil {
if s1, ok := p1.(generic.Stream); ok {
// verbose error handling
cause := err
if e, ok := err.(interface{ Cause() error }); ok {
cause = e.Cause()
}
switch cause {
case smux.ErrInvalidProtocol:
log.Println("smux version:1", err, "in:", fmt.Sprint(s1.RemoteAddr(), "(", s1.ID(), ")"), "out:", p2.RemoteAddr())
case smuxv2.ErrInvalidProtocol:
log.Println("smux version:2", err, "in:", fmt.Sprint(s1.RemoteAddr(), "(", s1.ID(), ")"), "out:", p2.RemoteAddr())
}
}
streamCopy := func(dst io.Writer, src io.ReadCloser) {
if _, err := generic.Copy(dst, src); err != nil {
if err == smux.ErrInvalidProtocol {
log.Println("smux", err, "in:", fmt.Sprint(p1.RemoteAddr(), "(", p1.ID(), ")"), "out:", p2.RemoteAddr())
}
xmitBuf.Put(buf)
close(die)
}()
return die
}
p1.Close()
p2.Close()
}
select {
case <-streamCopy(p1, p2):
case <-streamCopy(p2, p1):
}
go streamCopy(p2, p1)
streamCopy(p1, p2)
}
func checkError(err error) {
@@ -163,9 +124,6 @@ func main() {
// add more log flags for debugging
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
xmitBuf.New = func() interface{} {
return make([]byte, 32768)
}
myApp := cli.NewApp()
myApp.Name = "kcptun"
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"os/signal"
"syscall"
kcp "github.com/xtaci/kcp-go"
kcp "github.com/xtaci/kcp-go/v5"
)
func init() {