Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions hop/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@
package hop

import (
"sort"
"errors"
"sort"
"sync"
"time"
)

const (
hpBufSize = 384
hpBufSize = 384
bufferTimeout = 20 * time.Millisecond
)

type hopPacketBuffer struct {
buf [hpBufSize]*HopPacket
outQueue []*HopPacket
count int
timer *time.Timer
timeout time.Duration
buf [hpBufSize]*HopPacket
outQueue []*HopPacket
count int
timer *time.Timer
timeout time.Duration
flushChan chan *HopPacket
mutex sync.Mutex
mutex sync.Mutex
}

var bufFull = errors.New("Buffer Full")

func newHopPacketBuffer(flushChan chan *HopPacket, timeout time.Duration) *hopPacketBuffer {
hb := new(hopPacketBuffer)
hb.count = 0
hb.timer = time.NewTimer(1000*time.Second)
hb.timer = time.NewTimer(1000 * time.Second)
hb.timer.Stop()
hb.flushChan = flushChan
hb.timeout = timeout
Expand Down Expand Up @@ -86,7 +86,6 @@ func (hb *hopPacketBuffer) Swap(i, j int) {
hb.outQueue[i], hb.outQueue[j] = hb.outQueue[j], hb.outQueue[i]
}


func (hb *hopPacketBuffer) Flush() {
defer hb.mutex.Unlock()
hb.mutex.Lock()
Expand All @@ -105,7 +104,7 @@ func (hb *hopPacketBuffer) _flushToChan(c chan *HopPacket) {
}

sort.Sort(hb)
for _, p := range(hb.outQueue) {
for _, p := range hb.outQueue {
c <- p
}
hb.count = 0
Expand Down
8 changes: 5 additions & 3 deletions hop/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bytes"
"crypto/aes"
_cipher "crypto/cipher"
"crypto/md5"
"crypto/rand"
)

Expand All @@ -33,8 +34,9 @@ const cipherBlockSize = 16

func newHopCipher(key []byte) (*hopCipher, error) {
s := new(hopCipher)
key = PKCS5Padding(key, cipherBlockSize)
block, err := aes.NewCipher(key)
// key = PKCS5Padding(key, cipherBlockSize)
key1 := md5.Sum(key)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would cause incompability

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primarily for compatibility android. Because AES dynamic key_length is
difficult to coding in java. Therefore using 128 bit.

2014-12-30 16:08 GMT+08:00 bigeagle notifications@github.com:

In hop/cipher.go
#7 (diff):

@@ -33,8 +34,9 @@ const cipherBlockSize = 16

func newHopCipher(key []byte) (*hopCipher, error) {
s := new(hopCipher)

  • key = PKCS5Padding(key, cipherBlockSize)
  • block, err := aes.NewCipher(key)
  • // key = PKCS5Padding(key, cipherBlockSize)
  • key1 := md5.Sum(key)

this would cause incompability


Reply to this email directly or view it on GitHub
https://github.com/bigeagle/gohop/pull/7/files#r22341370.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有道理,另外这个也可以用于多用户支持,我考虑一下协议。

block, err := aes.NewCipher(key1[:])
if err != nil {
return nil, err
}
Expand All @@ -55,7 +57,7 @@ func (s *hopCipher) encrypt(msg []byte) []byte {
}

func (s *hopCipher) decrypt(iv []byte, ctext []byte) []byte {
defer func(){
defer func() {
if err := recover(); err != nil {
logger.Error("%v", err)
}
Expand Down
49 changes: 18 additions & 31 deletions hop/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
package hop

import (
"crypto/rand"
"errors"
"fmt"
"github.com/bigeagle/water"
mrand "math/rand"
"net"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
"fmt"
"errors"
"crypto/rand"
mrand "math/rand"
"github.com/bigeagle/water"
"sync/atomic"
)

var net_gateway, net_nic string
Expand All @@ -40,9 +40,9 @@ type route struct {

type HopClient struct {
// config
cfg HopClientConfig
cfg *HopClientConfig
// interface
iface *water.Interface
iface *water.Interface
// ip addr
ip net.IP

Expand All @@ -54,12 +54,12 @@ type HopClient struct {
// net to interface
toIface chan *HopPacket
// buffer for packets from net
recvBuf *hopPacketBuffer
recvBuf *hopPacketBuffer
// channel to send frames to net
toNet chan *HopPacket

handshakeDone chan byte
finishAck chan byte
finishAck chan byte
// state variable to ensure serverRoute added
srvRoute int32
// routes need to be clean in the end
Expand All @@ -68,8 +68,7 @@ type HopClient struct {
seq uint32
}


func NewClient(cfg HopClientConfig) error {
func NewClient(cfg *HopClientConfig) error {
var err error

// logger.Debug("%v", cfg)
Expand Down Expand Up @@ -122,7 +121,6 @@ func NewClient(cfg HopClientConfig) error {
go hopClient.handleUDP(server)
}


// wait until handshake done
res := <-hopClient.handshakeDone
if res == 0 {
Expand Down Expand Up @@ -193,7 +191,7 @@ func (clt *HopClient) handleInterface() {
} else {
// with traffic morphing
packets := hopFrager.Fragmentate(clt, buf[HOP_HDR_LEN:])
for _, hp := range(packets) {
for _, hp := range packets {
clt.toNet <- hp
}
}
Expand All @@ -211,7 +209,7 @@ func (clt *HopClient) handleUDP(server string) {
pktHandle := map[byte](func(*net.UDPConn, *HopPacket)){
HOP_FLG_HSH | HOP_FLG_ACK: clt.handleHandshakeAck,
HOP_FLG_HSH | HOP_FLG_FIN: clt.handleHandshakeError,
HOP_FLG_DAT: clt.handleDataPacket,
HOP_FLG_DAT: clt.handleDataPacket,
HOP_FLG_DAT | HOP_FLG_MFR: clt.handleDataPacket,
HOP_FLG_FIN | HOP_FLG_ACK: clt.handleFinishAck,
}
Expand Down Expand Up @@ -250,7 +248,6 @@ func (clt *HopClient) handleUDP(server string) {
}
}()


buf := make([]byte, IFACE_BUFSIZE)
for {
logger.Debug("waiting for udp packet")
Expand Down Expand Up @@ -284,7 +281,7 @@ func (clt *HopClient) toServer(u *net.UDPConn, flag byte, payload []byte, noise
hp.Seq = clt.Seq()
hp.setPayload(payload)
if noise {
hp.addNoise(mrand.Intn(MTU-64-len(payload)))
hp.addNoise(mrand.Intn(MTU - 64 - len(payload)))
}
u.Write(hp.Pack())
}
Expand Down Expand Up @@ -318,19 +315,12 @@ func (clt *HopClient) finishSession() {
clt.toNet <- hp
}


// handle handeshake ack
func (clt *HopClient) handleHandshakeAck(u *net.UDPConn, hp *HopPacket) {
if atomic.LoadInt32(&clt.state) == HOP_STAT_HANDSHAKE {
_ip, _net, _mask := make([]byte, 4), make([]byte, 4), make([]byte, 4)
copy(_ip, hp.payload[:4])
copy(_net, hp.payload[:4])
copy(_mask, hp.payload[4:8])
logger.Debug("%v", hp.payload)
_net[3] = 0

ip := net.IP(_ip)
subnet := &net.IPNet{_net, _mask}
by := hp.payload
addrStr := fmt.Sprintf("%d.%d.%d.%d/%d", by[0], by[1], by[2], by[3], by[4])
ip, subnet, _ := net.ParseCIDR(addrStr)
setTunIP(clt.iface, ip, subnet)
if clt.cfg.FixMSS {
fixMSS(clt.iface.Name(), false)
Expand All @@ -344,15 +334,14 @@ func (clt *HopClient) handleHandshakeAck(u *net.UDPConn, hp *HopPacket) {
}

logger.Debug("Handshake Ack to Server")
clt.toServer(u, HOP_FLG_HSH | HOP_FLG_ACK, clt.sid[:], true)
clt.toServer(u, HOP_FLG_HSH|HOP_FLG_ACK, clt.sid[:], true)
}

// handle handshake fail
func (clt *HopClient) handleHandshakeError(u *net.UDPConn, hp *HopPacket) {
clt.handshakeDone <- 0
}


// handle data packet
func (clt *HopClient) handleDataPacket(u *net.UDPConn, hp *HopPacket) {
// logger.Debug("New HopPacket Seq: %d", packet.Seq)
Expand All @@ -364,7 +353,6 @@ func (clt *HopClient) handleFinishAck(u *net.UDPConn, hp *HopPacket) {
clt.finishAck <- byte(1)
}


func (clt *HopClient) cleanUp() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
Expand All @@ -379,7 +367,6 @@ func (clt *HopClient) cleanUp() {
clearMSS(clt.iface.Name(), false)
}


timeout := time.After(3 * time.Second)
if clt.state != HOP_STAT_INIT {
clt.finishSession()
Expand Down
2 changes: 1 addition & 1 deletion hop/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package hop

import (
"github.com/bigeagle/gohop/logging"
"../logging"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go 官方不建议使用相对路径 https://code.google.com/p/go/issues/detail?id=6147

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的。这个之前没有注意。多谢。。我会修改的。

2014-12-30 19:09 GMT+08:00 bigeagle notifications@github.com:

In hop/common.go
#7 (diff):

@@ -19,7 +19,7 @@
package hop

import (

  • "github.com/bigeagle/gohop/logging"
  • "../logging"

go 官方不建议使用相对路径 https://code.google.com/p/go/issues/detail?id=6147


Reply to this email directly or view it on GitHub
https://github.com/bigeagle/gohop/pull/7/files#r22344766.

)

var logger = logging.GetLogger()
Expand Down
80 changes: 63 additions & 17 deletions hop/config.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
package hop

import (
"errors"
"bufio"
"code.google.com/p/gcfg"
"errors"
"fmt"
"io"
"regexp"
"strconv"
"strings"
)

// Server Config
type HopServerConfig struct {
HopStart int
HopEnd int
ListenAddr string
Addr string
MTU int
Key string
FixMSS bool
HopStart int
HopEnd int
ListenAddr string
Addr string
DNS []string
PeerTimeout int
MTU int
Key string
FixMSS bool
MorphMethod string
RouteFile string
RouteList map[uint32][][5]byte
}

// Client Config
type HopClientConfig struct {
Server string
HopStart int
HopEnd int
Key string
MTU int
FixMSS bool
Local bool
MorphMethod string
Server string
HopStart int
HopEnd int
Key string
MTU int
FixMSS bool
Local bool
MorphMethod string
Redirect_gateway bool
Net_gateway []string
Net_gateway []string
}

type HopConfig struct {
Expand All @@ -53,3 +64,38 @@ func ParseHopConfig(filename string) (interface{}, error) {
return nil, errors.New("Wrong mode")
}
}

func (cfg *HopServerConfig) RouteConfig(r io.Reader) {

regCM := regexp.MustCompile(`#.*`)
regIP := regexp.MustCompile(`(\d+)\.(\d+)\.(\d+)\.(\d+)\/(\d+)`)
regGM := regexp.MustCompile(`\[\s*(\d+)\s*\]`)
key := uint32(0)
ip := [5]byte{}
cfg.RouteList = make(map[uint32][][5]byte)

bf := bufio.NewReader(r)
for {
if line, err := bf.ReadString('\n'); err == nil {
if line = regCM.ReplaceAllString(line, ""); len(strings.TrimSpace(line)) < 3 {
continue
}
if ips := regIP.FindStringSubmatch(line); len(ips) == 6 {
for k, ipt := range ips[1:] {
v, _ := strconv.Atoi(ipt)
ip[k] = byte(v)
if k == 5 && uint8(ip[k]) > 32 {
ip[k] = 32
}
}
cfg.RouteList[key] = append(cfg.RouteList[key], ip)
} else if gms := regGM.FindStringSubmatch(line); len(gms) == 2 {
v, _ := strconv.Atoi(gms[1])
key = uint32(v)
}
} else {
break
}
}
logger.Debug(fmt.Sprintf("Route list : %v", cfg.RouteList))
}
Loading