Add tcp support

This commit is contained in:
wweir
2018-12-17 07:41:34 +08:00
parent 002ac49e65
commit 578d9162aa
9 changed files with 62 additions and 6 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ var Conf = struct {
func init() {
flag.StringVar(&Conf.ConfigFile, "f", "", "config file location")
flag.StringVar(&Conf.NetType, "n", "QUIC", "proxy net type (QUIC|KCP)")
flag.StringVar(&Conf.NetType, "n", "QUIC", "proxy net type (QUIC|KCP|TCP)")
flag.StringVar(&Conf.Password, "p", "12345678", "password")
flag.StringVar(&Conf.ServerPort, "P", "5533", "server mode listen port")
flag.StringVar(&Conf.ServerAddr, "s", "", "server IP (run in client mode if set)")
+1 -1
View File
@@ -1,4 +1,4 @@
package parser
package parse
import (
"bufio"
+1 -1
View File
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package parser
package parse
import (
"encoding/binary"
+1 -1
View File
@@ -1,4 +1,4 @@
package parser
package parse
import "net"
+3
View File
@@ -6,6 +6,7 @@ import (
"github.com/golang/glog"
"github.com/wweir/sower/proxy/kcp"
"github.com/wweir/sower/proxy/quic"
"github.com/wweir/sower/proxy/tcp"
)
type Client interface {
@@ -20,6 +21,8 @@ func StartClient(netType, server, password string) {
client = quic.NewClient()
case KCP.String():
client = kcp.NewClient(password)
case TCP.String():
client = tcp.NewClient()
}
for {
+5 -2
View File
@@ -8,19 +8,22 @@ import (
"github.com/wweir/sower/parse"
"github.com/wweir/sower/proxy/kcp"
"github.com/wweir/sower/proxy/quic"
"github.com/wweir/sower/proxy/tcp"
)
type Server interface {
Listen(port string) (<-chan net.Conn, error)
}
func StartServer(netType, port,password string) {
func StartServer(netType, port, password string) {
var server Server
switch netType {
case QUIC.String():
server = quic.NewServer()
case KCP.String():
server = kcp.NewServer(password)
case TCP.String():
server = tcp.NewServer()
}
if port == "" {
@@ -43,7 +46,7 @@ func StartServer(netType, port,password string) {
func handle(conn net.Conn) {
defer conn.Close()
conn, addr, err := parser.ParseAddr(conn)
conn, addr, err := parse.ParseAddr(conn)
if err != nil {
glog.Warningln(err)
return
+14
View File
@@ -0,0 +1,14 @@
package tcp
import "net"
type client struct {
}
func NewClient() *client {
return &client{}
}
func (c *client) Dial(server string) (net.Conn, error) {
return net.Dial("tcp", server)
}
+35
View File
@@ -0,0 +1,35 @@
package tcp
import (
"net"
"github.com/golang/glog"
)
type server struct {
}
func NewServer() *server {
return &server{}
}
func (s *server) Listen(port string) (<-chan net.Conn, error) {
ln, err := net.Listen("tcp", port)
if err != nil {
return nil, err
}
connCh := make(chan net.Conn)
go func() {
for {
conn, err := ln.Accept()
if err != nil {
glog.Errorln(err)
continue
}
connCh <- conn
}
}()
return connCh, nil
}
+1
View File
@@ -16,6 +16,7 @@ type netType int
const (
QUIC netType = iota
KCP
TCP
)
func relay(conn1, conn2 net.Conn) {