-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52998a7
commit a2da493
Showing
7 changed files
with
249 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package mobile | ||
|
||
import ( | ||
"github.com/mythologyli/zju-connect/client" | ||
"github.com/mythologyli/zju-connect/log" | ||
"github.com/mythologyli/zju-connect/stack/tun" | ||
) | ||
|
||
var vpnClient *client.EasyConnectClient | ||
|
||
func Login(server string, username string, password string) string { | ||
log.Init() | ||
|
||
vpnClient = client.NewEasyConnectClient( | ||
server, | ||
username, | ||
password, | ||
"", | ||
false, | ||
false, | ||
) | ||
err := vpnClient.Setup() | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
log.Printf("EasyConnect client started") | ||
|
||
clientIP, err := vpnClient.IP() | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return clientIP.String() | ||
} | ||
|
||
func StartStack(fd int) { | ||
vpnTUNStack, err := tun.NewStack(vpnClient, "") | ||
if err != nil { | ||
return | ||
} | ||
|
||
vpnTUNStack.SetupTun(fd) | ||
vpnTUNStack.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package tun | ||
|
||
import ( | ||
"context" | ||
"github.com/mythologyli/zju-connect/client" | ||
"github.com/mythologyli/zju-connect/log" | ||
"golang.org/x/net/ipv4" | ||
"io" | ||
"net" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
type Stack struct { | ||
endpoint *Endpoint | ||
rvpnConn io.ReadWriteCloser | ||
} | ||
|
||
func (s *Stack) Run() { | ||
var err error | ||
s.rvpnConn, err = client.NewRvpnConn(s.endpoint.easyConnectClient) | ||
if err != nil { | ||
log.Printf("Error occurred while creating sendConn: %v", err) | ||
return | ||
} | ||
|
||
ctxRead, cancelRead := context.WithCancel(context.Background()) | ||
ctxWrite, cancelWrite := context.WithCancel(context.Background()) | ||
|
||
// Read from VPN server and send to TUN stack | ||
go func() { | ||
for { | ||
select { | ||
case <-ctxRead.Done(): | ||
cancelWrite() | ||
return | ||
default: | ||
buf := make([]byte, 1500) | ||
n, err := s.rvpnConn.Read(buf) | ||
if err != nil { | ||
cancelWrite() | ||
return | ||
} | ||
|
||
log.DebugPrintf("Recv: read %d bytes", n) | ||
log.DebugDumpHex(buf[:n]) | ||
|
||
err = s.endpoint.Write(buf[:n]) | ||
if err != nil { | ||
log.Printf("Error occurred while writing to TUN stack: %v", err) | ||
cancelWrite() | ||
return | ||
} | ||
} | ||
} | ||
}() | ||
|
||
// Read from TUN stack and send to VPN server | ||
for { | ||
select { | ||
case <-ctxWrite.Done(): | ||
cancelRead() | ||
return | ||
default: | ||
buf := make([]byte, 1500) | ||
n, err := s.endpoint.Read(buf) | ||
if err != nil { | ||
log.Printf("Error occurred while reading from TUN stack: %v", err) | ||
cancelRead() | ||
return | ||
} | ||
|
||
if n < 20 { | ||
continue | ||
} | ||
|
||
header, err := ipv4.ParseHeader(buf[:n]) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
// Filter out non-TCP/UDP packets otherwise error may occur | ||
if header.Protocol != syscall.IPPROTO_TCP && header.Protocol != syscall.IPPROTO_UDP { | ||
continue | ||
} | ||
|
||
_, err = s.rvpnConn.Write(buf[:n]) | ||
if err != nil { | ||
cancelRead() | ||
return | ||
} | ||
|
||
log.DebugPrintf("Send: wrote %d bytes", n) | ||
log.DebugDumpHex(buf[:n]) | ||
} | ||
} | ||
} | ||
|
||
type Endpoint struct { | ||
easyConnectClient *client.EasyConnectClient | ||
|
||
readWriteCloser io.ReadWriteCloser | ||
ip net.IP | ||
|
||
tcpDialer *net.Dialer | ||
udpDialer *net.Dialer | ||
} | ||
|
||
func (ep *Endpoint) Write(buf []byte) error { | ||
_, err := ep.readWriteCloser.Write(buf) | ||
return err | ||
} | ||
|
||
func (ep *Endpoint) Read(buf []byte) (int, error) { | ||
return ep.readWriteCloser.Read(buf) | ||
} | ||
|
||
func (s *Stack) AddRoute(target string) error { | ||
return nil | ||
} | ||
|
||
func NewStack(easyConnectClient *client.EasyConnectClient, dnsServer string) (*Stack, error) { | ||
s := &Stack{} | ||
|
||
s.endpoint = &Endpoint{ | ||
easyConnectClient: easyConnectClient, | ||
} | ||
|
||
var err error | ||
s.endpoint.ip, err = easyConnectClient.IP() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// We need this dialer to bind to device otherwise packets will not be sent via TUN | ||
s.endpoint.tcpDialer = &net.Dialer{ | ||
LocalAddr: &net.TCPAddr{ | ||
IP: s.endpoint.ip, | ||
Port: 0, | ||
}, | ||
} | ||
|
||
s.endpoint.udpDialer = &net.Dialer{ | ||
LocalAddr: &net.UDPAddr{ | ||
IP: s.endpoint.ip, | ||
Port: 0, | ||
}, | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
func (s *Stack) SetupTun(fd int) { | ||
s.endpoint.readWriteCloser = os.NewFile(uintptr(fd), "tun") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !android | ||
|
||
package tun | ||
|
||
import ( | ||
|