-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTunnel.go
More file actions
121 lines (81 loc) · 2.51 KB
/
Copy pathTunnel.go
File metadata and controls
121 lines (81 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package https
import "tholian-endpoint/protocols/dns"
import "tholian-endpoint/protocols/http"
import "tholian-endpoint/types"
import http_tunnel "tholian-warps/protocols/http/tunnel"
import "crypto/tls"
import net_url "net/url"
import "strconv"
type Tunnel struct {
Host string `json:"host"`
Port uint16 `json:"port"`
certificate *tls.Certificate
}
func NewTunnel(host string, port uint16) Tunnel {
var tunnel Tunnel
tunnel.Host = host
tunnel.Port = port
tunnel.certificate = nil
return tunnel
}
func (tunnel *Tunnel) ResolvePacket(query dns.Packet) dns.Packet {
var response dns.Packet
url, err := net_url.Parse("https://" + tunnel.Host + ":" + strconv.FormatUint(uint64(tunnel.Port), 10) + "/dns-query")
if err == nil {
tunnel_request := http.NewPacket()
tunnel_request.SetURL(*url)
if tunnel.certificate != nil {
tunnel_request.SetCertificate(*tunnel.certificate.Leaf)
}
http_tunnel.EncodePayload(&tunnel_request, query.Bytes())
tunnel_response := http.RequestPacket(tunnel_request)
if http_tunnel.IsResolveResponse(&tunnel_response) {
response = dns.Parse(http_tunnel.DecodePayload(&tunnel_response))
} else {
response = dns.NewPacket()
response.SetType("response")
response.SetIdentifier(query.Identifier)
response.SetResponseCode(dns.ResponseCodeNonExistDomain)
for q := 0; q < len(query.Questions); q++ {
response.AddQuestion(query.Questions[q])
}
}
} else {
response = dns.NewPacket()
response.SetType("response")
response.SetIdentifier(query.Identifier)
response.SetResponseCode(dns.ResponseCodeNonExistDomain)
for q := 0; q < len(query.Questions); q++ {
response.AddQuestion(query.Questions[q])
}
}
return response
}
func (tunnel *Tunnel) RequestPacket(request http.Packet) http.Packet {
var response http.Packet
tunnel_request := request
tunnel_request.SetServer(types.Server{
Domain: tunnel.Host,
Addresses: []string{tunnel.Host},
Port: tunnel.Port,
Protocol: types.ProtocolHTTPS,
Schema: "DEFAULT",
})
if tunnel.certificate != nil {
tunnel_request.SetCertificate(*tunnel.certificate.Leaf)
}
tunnel_response := http.RequestPacket(tunnel_request)
if tunnel_response.Type == "response" {
response = tunnel_response
response.Decode()
} else {
response = http.NewPacket()
response.SetURL(*request.URL)
response.SetStatus(http.StatusNotFound)
response.SetPayload([]byte{})
}
return response
}
func (tunnel *Tunnel) SetCertificate(value tls.Certificate) {
tunnel.certificate = &value
}