Skip to content

Commit

Permalink
move fix mss to gohop server
Browse files Browse the repository at this point in the history
  • Loading branch information
bigeagle committed Dec 10, 2013
1 parent bddc4ac commit 12610e9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
4 changes: 2 additions & 2 deletions hop/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}


Expand Down
1 change: 1 addition & 0 deletions hop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ type HopServerConfig struct {
Addr string
MTU int
Key string
FixMSS bool
MorphMethod string
}

Expand Down
19 changes: 15 additions & 4 deletions hop/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
27 changes: 16 additions & 11 deletions hop/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"bytes"
"fmt"
"net"
// "os"
// "os/signal"
// "syscall"
"os"
"os/signal"
"syscall"
"time"
"sync/atomic"
"sync"
Expand Down Expand Up @@ -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":
Expand All @@ -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++ {
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 12610e9

Please sign in to comment.