Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add outline-go-tun2socks, except android #1744

Merged
merged 78 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
623f690
init tun2socks
daniellacosse Oct 5, 2023
6958582
Merge branch 'master' into daniellacosse/go_tun2socks
daniellacosse Oct 9, 2023
38fd959
move to src/tun2socks
daniellacosse Oct 9, 2023
9079f6e
addt'l feedback
daniellacosse Oct 9, 2023
33c05c3
wip: working on build script
daniellacosse Oct 9, 2023
766516c
Merge branch 'master' into daniellacosse/go_tun2socks
daniellacosse Oct 9, 2023
e037b03
Merge branch 'master' into daniellacosse/go_tun2socks
daniellacosse Nov 13, 2023
1a4d61e
update go.mod and references
daniellacosse Nov 13, 2023
6ef1700
attempt #1 adding to build process
daniellacosse Nov 13, 2023
d8440d2
cleanup
daniellacosse Nov 13, 2023
50bf620
update CI
daniellacosse Nov 13, 2023
ee66c4d
okay it all works
daniellacosse Nov 13, 2023
b28d736
setup go
daniellacosse Nov 14, 2023
4524856
try gomobile init
daniellacosse Nov 14, 2023
a4ff501
build go binaries instead
daniellacosse Nov 14, 2023
a290d1c
try env.PATH
daniellacosse Nov 14, 2023
70078ca
gah just inline it lol
daniellacosse Nov 14, 2023
2611e58
build go binaries in action and revert github config
daniellacosse Nov 14, 2023
c01cb21
make the bindir
daniellacosse Nov 14, 2023
409d09f
properly link bins for electron
daniellacosse Nov 14, 2023
4cc2732
scope folder
daniellacosse Nov 16, 2023
0424f6e
copy android lib, improve spawn stream error log
daniellacosse Nov 16, 2023
71bfeae
apparently we have to do this
daniellacosse Nov 16, 2023
51fea86
try this
daniellacosse Nov 16, 2023
5f9989a
try -out
daniellacosse Nov 20, 2023
6604455
safer paths
daniellacosse Nov 20, 2023
5ea79d5
try goos android
daniellacosse Nov 20, 2023
5f41f48
this?
daniellacosse Nov 20, 2023
ccb4dad
topsy turvy upside down day
daniellacosse Nov 20, 2023
d5333bf
oops
daniellacosse Nov 20, 2023
0c48e70
detect os platforms
daniellacosse Nov 20, 2023
ad60a33
revert ci os container changes
daniellacosse Nov 20, 2023
9374e15
build windows on mac??
daniellacosse Nov 20, 2023
9eca094
oh derp
daniellacosse Nov 20, 2023
c337edf
take into consideration host platform
daniellacosse Nov 21, 2023
f2bb095
oops, revert mac change
daniellacosse Nov 21, 2023
1b8fbd6
fix setup_linux_android
daniellacosse Nov 21, 2023
ca554cd
try ldf flags
daniellacosse Nov 21, 2023
182a94d
remove ldf
daniellacosse Nov 21, 2023
3de7cec
update mac location and add tunnel target to linux/android
daniellacosse Nov 21, 2023
0d437ca
oops forgot target
daniellacosse Nov 21, 2023
7f67fbc
try go:build android and improve error output missing message
daniellacosse Nov 21, 2023
1477913
Merge branch 'master' into daniellacosse/go_tun2socks
daniellacosse Nov 27, 2023
119f0a3
build tun2socks before apple tests
daniellacosse Nov 27, 2023
64ebd35
not sure this will work in the CI, but hey
daniellacosse Nov 27, 2023
f4a6b95
so weird lol
daniellacosse Nov 29, 2023
5f63c5c
apple fix??
daniellacosse Nov 29, 2023
a056a85
builder path issue
daniellacosse Nov 29, 2023
d0075f5
revert android build
daniellacosse Dec 6, 2023
ed5f8ba
Update tools.go
daniellacosse Dec 6, 2023
aee2ea1
ios simulator, go 1.20
daniellacosse Dec 6, 2023
559e64c
Update build_and_test_debug.yml
daniellacosse Dec 8, 2023
df0b01b
update versions and remove build folder
daniellacosse Dec 8, 2023
fa078f0
update engine??
daniellacosse Dec 8, 2023
5d3aeee
engines dot x
daniellacosse Dec 8, 2023
eacaad5
additional cleanup
daniellacosse Dec 8, 2023
1e3b016
Update src/electron/README.md
daniellacosse Dec 11, 2023
c804b83
Update src/tun2socks/build.action.mjs
daniellacosse Dec 11, 2023
0c61a37
Update build_and_test_debug.yml
daniellacosse Dec 11, 2023
121147b
use go file and split ios/macos calls
daniellacosse Dec 11, 2023
6157685
create intermediary tun2socks folder and try to fix electron pathing …
daniellacosse Dec 11, 2023
e96091a
Update README.md
daniellacosse Dec 11, 2023
59141c4
this should fix linux? still working out windows
daniellacosse Dec 12, 2023
6e1906a
did I revert it correctly? 😅
daniellacosse Dec 13, 2023
b3bb834
now i'm all confused...
daniellacosse Dec 13, 2023
0162c76
this?
daniellacosse Dec 13, 2023
a4cc2e8
upgrade gomobile and add macos to the thing
daniellacosse Dec 14, 2023
c71236c
Merge branch 'master' into daniellacosse/go_tun2socks
daniellacosse Dec 14, 2023
ddcd75d
add TODOs
daniellacosse Dec 15, 2023
c17a7b0
use makefile
daniellacosse Jan 4, 2024
e8d3aa9
Merge branch 'master' into daniellacosse/go_tun2socks
daniellacosse Jan 4, 2024
9b6b1ce
update IMPORT_PATH
daniellacosse Jan 8, 2024
2195bd2
split up pkg names
daniellacosse Jan 8, 2024
cfd2130
?
daniellacosse Jan 8, 2024
2beffe7
??
daniellacosse Jan 8, 2024
a6f0348
Merge branch 'master' into daniellacosse/go_tun2socks
daniellacosse Jan 8, 2024
35b3b44
set androida api
daniellacosse Jan 10, 2024
37f9e14
add todos
daniellacosse Jan 10, 2024
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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* @Jigsaw-Code/outline-dev

/outline/tun2socks/ @Jigsaw-Code/outline-networking-owners
/src/cordova/plugin/ @Jigsaw-Code/outline-networking-owners
/third_party/ @Jigsaw-Code/outline-networking-owners
/tools/ @Jigsaw-Code/outline-networking-owners
Expand Down
30 changes: 29 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,32 @@ Outline.apks
universal.apk
toc.pb
coverage
*.env
*.env
/build
/bin

daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
# General
.AppleDouble
.LSOverride

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# IDEs
.vscode/
23 changes: 15 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ module github.com/Jigsaw-Code/outline-apps
go 1.20

require (
github.com/Jigsaw-Code/outline-sdk v0.0.2
github.com/Jigsaw-Code/outline-sdk/x v0.0.0-20230807220427-893de7fdc6b8
github.com/Jigsaw-Code/choir v1.0.1
github.com/Jigsaw-Code/getsni v1.0.0
github.com/Jigsaw-Code/outline-sdk v0.0.7
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
github.com/Jigsaw-Code/outline-sdk/x v0.0.7
github.com/crazy-max/xgo v0.26.0
github.com/eycorsican/go-tun2socks v1.16.11
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.11.0
golang.org/x/mobile v0.0.0-20230906132913-2077a3224571
golang.org/x/net v0.15.0
golang.org/x/sys v0.12.0
golang.org/x/sys/windows/registry v0.0.0-20230906132913-2077a3224571
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/eycorsican/go-tun2socks v1.16.11 // indirect
github.com/miekg/dns v1.1.54 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 // indirect
github.com/shadowsocks/go-shadowsocks2 v0.1.5 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/tools v0.9.1 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
39 changes: 26 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
github.com/Jigsaw-Code/outline-sdk v0.0.2 h1:uCuyJMaWj57IYEG/Hdml8YMdk9chU60ZkSxJXBhyGHU=
github.com/Jigsaw-Code/outline-sdk v0.0.2/go.mod h1:hhlKz0+r9wSDFT8usvN8Zv/BFToCIFAUn1P2Qk8G2CM=
github.com/Jigsaw-Code/choir v1.0.1 h1:WeRt6aTn5L+MtRNqRJ+J1RKgoO8CyXXt1dtZghy2KjE=
github.com/Jigsaw-Code/choir v1.0.1/go.mod h1:c4Wd1y1PeCajZbKZV+ZmcFGMDoduyqMCEMHW5iqzWXI=
github.com/Jigsaw-Code/getsni v1.0.0 h1:OUTIu7wTBi/7DMX+RkZrN7XhU3UDevTEsAWK4gsqSwE=
github.com/Jigsaw-Code/getsni v1.0.0/go.mod h1:Ps0Ec3fVMKLyAItVbMKoQFq1lDjtFQXZ+G5nRNNh/QE=
github.com/Jigsaw-Code/outline-sdk v0.0.7 h1:WlFaV1tFpIQ/pflrKwrQuNIP3kJpgh7yJuqiTb54sGA=
github.com/Jigsaw-Code/outline-sdk v0.0.7/go.mod h1:hhlKz0+r9wSDFT8usvN8Zv/BFToCIFAUn1P2Qk8G2CM=
github.com/Jigsaw-Code/outline-sdk/x v0.0.0-20230807220427-893de7fdc6b8 h1:BxOHmmuppPM8K0DGUsfvajKF4PKfGxv9boNDhmbszFU=
github.com/Jigsaw-Code/outline-sdk/x v0.0.0-20230807220427-893de7fdc6b8/go.mod h1:tBqJXpVm+kym+EAUdwNodcFxy872FfjVErfj8Br+gs0=
github.com/crazy-max/xgo v0.26.0 h1:vK4OfeXJoDGvnjlzdTCgPbeWLKENbzj84DTpU/VRonM=
github.com/crazy-max/xgo v0.26.0/go.mod h1:m/aqfKaN/cYzfw+Pzk7Mk0tkmShg3/rCS4Zdhdugi4o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eycorsican/go-tun2socks v1.16.11 h1:+hJDNgisrYaGEqoSxhdikMgMJ4Ilfwm/IZDrWRrbaH8=
Expand All @@ -18,27 +24,34 @@ github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3/go.mod h1:HgjTstv
github.com/shadowsocks/go-shadowsocks2 v0.1.5 h1:PDSQv9y2S85Fl7VBeOMF9StzeXZyK1HakRm86CUbr28=
github.com/shadowsocks/go-shadowsocks2 v0.1.5/go.mod h1:AGGpIoek4HRno4xzyFiAtLHkOpcoznZEkAccaI/rplM=
github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b/go.mod h1:P5HUIBuIWKbyjl083/loAegFkfbFNx5i2qEP4CNbm7E=
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 h1:TG/diQgUe0pntT/2D9tmUCz4VNwm9MfrtPr0SU2qSX8=
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8/go.mod h1:P5HUIBuIWKbyjl083/loAegFkfbFNx5i2qEP4CNbm7E=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/mobile v0.0.0-20230906132913-2077a3224571 h1:QDvQ2KLFHHQWRID6IkZOBf6uLIh9tZ0G+mw61pFQxuo=
golang.org/x/mobile v0.0.0-20230906132913-2077a3224571/go.mod h1:wEyOn6VvNW7tcf+bW/wBz1sehi2s2BZ4TimyR7qZen4=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20191021144547-ec77196f6094/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
golang.org/x/tools v0.9.1/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
1 change: 1 addition & 0 deletions src/tun2socks/build.action.mjs
fortuna marked this conversation as resolved.
Show resolved Hide resolved
fortuna marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO
27 changes: 27 additions & 0 deletions src/tun2socks/outline/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2023 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package outline

import (
"github.com/Jigsaw-Code/outline-sdk/transport"
)

// Client provides a transparent container for [transport.StreamDialer] and [transport.PacketListener]
// that is exportable (as an opaque object) via gobind.
// It's used by the connectivity test and the tun2socks handlers.
type Client struct {
transport.StreamDialer
transport.PacketListener
}
157 changes: 157 additions & 0 deletions src/tun2socks/outline/connectivity/connectivity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Copyright 2023 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package connectivity

import (
"context"
"errors"
"net"
"net/http"
"time"

"github.com/Jigsaw-Code/outline-apps/src/tun2socks/outline"
"github.com/Jigsaw-Code/outline-apps/src/tun2socks/outline/neterrors"
"github.com/Jigsaw-Code/outline-sdk/transport"
)

// TODO: make these values configurable by exposing a struct with the connectivity methods.
const (
tcpTimeout = 10 * time.Second
udpTimeout = 1 * time.Second
udpMaxRetryAttempts = 5
bufferLength = 512
)

// authenticationError is used to signal failed authentication to the Shadowsocks proxy.
type authenticationError struct {
error
}

// reachabilityError is used to signal an unreachable proxy.
type reachabilityError struct {
error
}

// CheckConnectivity determines whether the Shadowsocks proxy can relay TCP and UDP traffic under
// the current network. Parallelizes the execution of TCP and UDP checks, selects the appropriate
// error code to return accounting for transient network failures.
// Returns an error if an unexpected error ocurrs.
func CheckConnectivity(client *outline.Client) (neterrors.Error, error) {
// Start asynchronous UDP support check.
udpChan := make(chan error)
go func() {
resolverAddr := &net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: 53}
udpChan <- CheckUDPConnectivityWithDNS(client, resolverAddr)
}()
// Check whether the proxy is reachable and that the client is able to authenticate to the proxy
tcpErr := CheckTCPConnectivityWithHTTP(client, "http://example.com")
if tcpErr == nil {
udpErr := <-udpChan
if udpErr == nil {
return neterrors.NoError, nil
}
return neterrors.UDPConnectivity, nil
}
var authErr *authenticationError
var reachabilityErr *reachabilityError
if errors.As(tcpErr, &authErr) {
return neterrors.AuthenticationFailure, nil
} else if errors.As(tcpErr, &reachabilityErr) {
return neterrors.Unreachable, nil
}
// The error is not related to the connectivity checks.
return neterrors.Unexpected, tcpErr
}

// CheckUDPConnectivityWithDNS determines whether the Shadowsocks proxy represented by `client` and
// the network support UDP traffic by issuing a DNS query though a resolver at `resolverAddr`.
// Returns nil on success or an error on failure.
func CheckUDPConnectivityWithDNS(client transport.PacketListener, resolverAddr net.Addr) error {
conn, err := client.ListenPacket(context.Background())
if err != nil {
return err
}
defer conn.Close()
buf := make([]byte, bufferLength)
for attempt := 0; attempt < udpMaxRetryAttempts; attempt++ {
conn.SetDeadline(time.Now().Add(udpTimeout))
_, err := conn.WriteTo(getDNSRequest(), resolverAddr)
if err != nil {
continue
}
n, addr, err := conn.ReadFrom(buf)
if n == 0 && err != nil {
continue
}
if addr.String() != resolverAddr.String() {
continue // Ensure we got a response from the resolver.
}
return nil
}
return errors.New("UDP connectivity check timed out")
}

// CheckTCPConnectivityWithHTTP determines whether the proxy is reachable over TCP and validates the
// client's authentication credentials by performing an HTTP HEAD request to `targetURL`, which must
// be of the form: http://[host](:[port])(/[path]). Returns nil on success, error if `targetURL` is
// invalid, AuthenticationError or ReachabilityError on connectivity failure.
func CheckTCPConnectivityWithHTTP(dialer transport.StreamDialer, targetURL string) error {
deadline := time.Now().Add(tcpTimeout)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
req, err := http.NewRequest("HEAD", targetURL, nil)
if err != nil {
return err
}
targetAddr := req.Host
if !hasPort(targetAddr) {
targetAddr = net.JoinHostPort(targetAddr, "80")
}
conn, err := dialer.Dial(ctx, targetAddr)
if err != nil {
return &reachabilityError{err}
}
defer conn.Close()
conn.SetDeadline(deadline)
err = req.Write(conn)
if err != nil {
return &authenticationError{err}
}
n, err := conn.Read(make([]byte, bufferLength))
if n == 0 && err != nil {
return &authenticationError{err}
}
return nil
}

func getDNSRequest() []byte {
return []byte{
0, 0, // [0-1] query ID
1, 0, // [2-3] flags; byte[2] = 1 for recursion desired (RD).
0, 1, // [4-5] QDCOUNT (number of queries)
0, 0, // [6-7] ANCOUNT (number of answers)
0, 0, // [8-9] NSCOUNT (number of name server records)
0, 0, // [10-11] ARCOUNT (number of additional records)
3, 'c', 'o', 'm',
0, // null terminator of FQDN (root TLD)
0, 1, // QTYPE, set to A
0, 1, // QCLASS, set to 1 = IN (Internet)
}
}

func hasPort(hostPort string) bool {
_, _, err := net.SplitHostPort(hostPort)
return err == nil
}
Loading
Loading