From 12610e9989b7754154ef3ccd2cece674570f40c8 Mon Sep 17 00:00:00 2001 From: bigeagle Date: Tue, 10 Dec 2013 16:04:57 +0800 Subject: [PATCH] move fix mss to gohop server --- hop/client.go | 4 ++-- hop/config.go | 1 + hop/iface.go | 19 +++++++++++++++---- hop/server.go | 27 ++++++++++++++++----------- main.go | 2 +- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/hop/client.go b/hop/client.go index ab382e3..c4c1b89 100644 --- a/hop/client.go +++ b/hop/client.go @@ -333,7 +333,7 @@ func (clt *HopClient) handleHandshakeAck(u *net.UDPConn, hp *HopPacket) { subnet := &net.IPNet{_net, _mask} setTunIP(clt.iface, ip, subnet) if clt.cfg.FixMSS { - fixMSS(clt.iface.Name()) + fixMSS(clt.iface.Name(), false) } res := atomic.CompareAndSwapInt32(&clt.state, HOP_STAT_HANDSHAKE, HOP_STAT_WORKING) if !res { @@ -376,7 +376,7 @@ func (clt *HopClient) cleanUp() { delRoute("128.0.0.0/1") } if clt.cfg.FixMSS { - clearMSS(clt.iface.Name()) + clearMSS(clt.iface.Name(), false) } diff --git a/hop/config.go b/hop/config.go index 04415d5..70248d6 100644 --- a/hop/config.go +++ b/hop/config.go @@ -208,6 +208,7 @@ type HopServerConfig struct { Addr string MTU int Key string + FixMSS bool MorphMethod string } diff --git a/hop/iface.go b/hop/iface.go index fb5e54b..d38beb2 100644 --- a/hop/iface.go +++ b/hop/iface.go @@ -232,10 +232,15 @@ func unredirectPort(from, to string) error { return nil } -func fixMSS(iface string) error { +func fixMSS(iface string, is_server bool) error { mss := MTU - 40 logger.Info("Fix MSS with iptables to %d", mss) - sargs := fmt.Sprintf("-I FORWARD -o %s -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss %d", iface, mss) + io := "o" + if is_server { + io = "i" + } + + sargs := fmt.Sprintf("-I FORWARD -%s %s -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss %d", io, iface, mss) args := strings.Split(sargs, " ") cmd := exec.Command("iptables", args...) err := cmd.Run() @@ -246,10 +251,16 @@ func fixMSS(iface string) error { return nil } -func clearMSS(iface string) error { +func clearMSS(iface string, is_server bool) error { mss := MTU - 40 logger.Info("Clean MSS fix", mss) - sargs := fmt.Sprintf("-D FORWARD -o %s -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss %d", iface, mss) + io := "o" + + if is_server { + io = "i" + } + sargs := fmt.Sprintf("-D FORWARD -%s %s -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss %d", io, iface, mss) + args := strings.Split(sargs, " ") cmd := exec.Command("iptables", args...) err := cmd.Run() diff --git a/hop/server.go b/hop/server.go index a20565c..0b534bc 100644 --- a/hop/server.go +++ b/hop/server.go @@ -25,9 +25,9 @@ import ( "bytes" "fmt" "net" -// "os" -// "os/signal" -// "syscall" + "os" + "os/signal" + "syscall" "time" "sync/atomic" "sync" @@ -101,6 +101,10 @@ func NewServer(cfg HopServerConfig) error { hopServer.ipnet = &net.IPNet{ip, subnet.Mask} hopServer.ippool.subnet = subnet + if cfg.FixMSS { + fixMSS(iface.Name(), true) + } + // traffic morpher switch cfg.MorphMethod { case "randsize": @@ -119,6 +123,7 @@ func NewServer(cfg HopServerConfig) error { // defer hopServer.cleanUp() // redirectPort(cfg.HopRange, cfg.Port) // }() + go hopServer.cleanUp() // serve for multiple ports for idx, port := 0, cfg.HopStart; port <= cfg.HopEnd; port++ { @@ -408,11 +413,11 @@ func (srv *HopServer) handleFinish(u *udpPacket, hp *HopPacket) { srv.toClient(hpeer, HOP_FLG_FIN | HOP_FLG_ACK, []byte{}, false) } -// func (srv *HopServer) cleanUp() { -// c := make(chan os.Signal, 1) -// signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) -// <-c -// -// unredirectPort(srv.cfg.HopRange, srv.cfg.Port) -// os.Exit(0) -// } +func (srv *HopServer) cleanUp() { + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) + <-c + + clearMSS(srv.iface.Name(), true) + os.Exit(0) +} diff --git a/main.go b/main.go index 2558fc3..4577087 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ import ( var srvMode, cltMode, debug, getVersion bool var cfgFile string -var VERSION = "0.3alpha1" +var VERSION = "0.3alpha2" func main() { flag.BoolVar(&getVersion, "version", false, "Get Version info")