-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Showing
13 changed files
with
185 additions
and
234 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "core/src/main/golang/clash"] | ||
path = core/src/main/golang/clash | ||
url = https://github.com/Kr328/Clash |
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,11 +1,11 @@ | ||
module github.com/kr328/clash | ||
module github.com/kr328/cfa | ||
|
||
go 1.13 | ||
|
||
require ( | ||
github.com/Dreamacro/clash v0.16.1-0.20191112020912-8e10e67b8925 | ||
github.com/google/btree v1.0.0 // indirect | ||
github.com/google/netstack v0.0.0-20191116005144-95bf25ab4723 | ||
golang.org/x/sys v0.0.0-20191118133127-cf1e2d577169 | ||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect | ||
github.com/Dreamacro/clash v0.0.0 // local | ||
github.com/google/netstack v0.0.0-20191031000057-4787376a6744 | ||
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe | ||
) | ||
|
||
replace github.com/Dreamacro/clash v0.0.0 => ./clash |
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
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 was deleted.
Oops, something went wrong.
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,115 @@ | ||
package tun | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/google/netstack/tcpip/adapters/gonet" | ||
"github.com/google/netstack/tcpip/buffer" | ||
"github.com/google/netstack/tcpip/stack" | ||
"github.com/google/netstack/tcpip/transport/udp" | ||
"github.com/google/netstack/waiter" | ||
) | ||
|
||
const defaultTimeout = 5 | ||
const defaultDNS = "8.8.8.8" | ||
|
||
type DnsRedirectEndpoint struct { | ||
stack *stack.Stack | ||
udpForwarder *udp.Forwarder | ||
targetAddr *net.UDPAddr | ||
} | ||
|
||
func NewDnsRedirect(s *stack.Stack) *DnsRedirectEndpoint { | ||
result := &DnsRedirectEndpoint{ | ||
stack: s, | ||
udpForwarder: nil, | ||
targetAddr: &net.UDPAddr{IP: net.ParseIP("8.8.8.8"), Port: 53}, | ||
} | ||
|
||
result.udpForwarder = udp.NewForwarder(s, func(request *udp.ForwarderRequest) { | ||
var wq waiter.Queue | ||
ep, err := request.CreateEndpoint(&wq) | ||
if err != nil { | ||
return | ||
} | ||
|
||
conn := gonet.NewConn(&wq, ep) | ||
|
||
targetConn, udpErr := net.DialUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0}, result.targetAddr) | ||
if udpErr != nil { | ||
conn.Close() | ||
return | ||
} | ||
|
||
// send | ||
go func() { | ||
var buffer [128]byte | ||
|
||
defer targetConn.Close() | ||
defer conn.Close() | ||
|
||
for { | ||
conn.SetDeadline(time.Now().Add(defaultTimeout * time.Second)) | ||
|
||
n, err := conn.Read(buffer[:]) | ||
if err != nil { | ||
return | ||
} | ||
|
||
_, err = targetConn.Write(buffer[:n]) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
}() | ||
|
||
// recive | ||
go func() { | ||
var buffer [128]byte | ||
|
||
defer targetConn.Close() | ||
defer conn.Close() | ||
|
||
for { | ||
conn.SetDeadline(time.Now().Add(defaultTimeout * time.Second)) | ||
|
||
n, err := targetConn.Read(buffer[:]) | ||
if err != nil { | ||
return | ||
} | ||
|
||
_, err = conn.Write(buffer[:n]) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
}() | ||
}) | ||
|
||
return result | ||
} | ||
|
||
func (d *DnsRedirectEndpoint) SetTargetAddress(addr *net.UDPAddr) { | ||
d.targetAddr = addr | ||
} | ||
|
||
func (d *DnsRedirectEndpoint) UniqueID() uint64 { | ||
return 999 | ||
} | ||
|
||
func (d *DnsRedirectEndpoint) HandlePacket(r *stack.Route, id stack.TransportEndpointID, pkt buffer.VectorisedView) { | ||
d.udpForwarder.HandlePacket(r, id, nil, pkt) | ||
} | ||
|
||
func (d *DnsRedirectEndpoint) HandleControlPacket(id stack.TransportEndpointID, typ stack.ControlType, extra uint32, pkt buffer.VectorisedView) { | ||
// Unsupported | ||
} | ||
|
||
func (d *DnsRedirectEndpoint) Close() { | ||
// Unsupported | ||
} | ||
|
||
func (d *DnsRedirectEndpoint) Wait() { | ||
// Unsupported | ||
} |
Oops, something went wrong.