Skip to content

Commit 3dd8e84

Browse files
committed
2025-08-19 14:44:24
1 parent 1cfcb37 commit 3dd8e84

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

daze.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ func (c Cdoh) Write(b []byte) (n int, err error) {
157157
// ResolverDoh returns a DoH resolver. For further information, see https://datatracker.ietf.org/doc/html/rfc8484.
158158
func ResolverDoh(addr string) *net.Resolver {
159159
urls := doa.Try(url.Parse(addr))
160-
urls.Host = doa.Try(net.LookupHost(urls.Hostname()))[0]
160+
host := doa.Try(net.LookupHost(urls.Hostname()))[0]
161+
port := urls.Port()
162+
urls.Host = host
163+
if port != "" {
164+
urls.Host = host + ":" + port
165+
}
161166
return &net.Resolver{
162167
PreferGo: true,
163168
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {

daze_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
const (
1313
DazeServerListenOn = "127.0.0.1:28080"
1414
CurlDest = "https://www.zhihu.com"
15+
HostLookup = "google.com"
1516
)
1617

1718
func TestLocaleHTTP(t *testing.T) {
@@ -64,23 +65,23 @@ func TestLocaleSocks5(t *testing.T) {
6465

6566
func TestResolverDns(t *testing.T) {
6667
dns := ResolverDns("1.1.1.1:53")
67-
_, err := dns.LookupHost(context.Background(), "google.com")
68+
_, err := dns.LookupHost(context.Background(), HostLookup)
6869
if err != nil {
6970
t.FailNow()
7071
}
7172
}
7273

7374
func TestResolverDot(t *testing.T) {
7475
dot := ResolverDot("1.1.1.1:853")
75-
_, err := dot.LookupHost(context.Background(), "google.com")
76+
_, err := dot.LookupHost(context.Background(), HostLookup)
7677
if err != nil {
7778
t.FailNow()
7879
}
7980
}
8081

8182
func TestResolverDoh(t *testing.T) {
8283
doh := ResolverDoh("https://1.1.1.1/dns-query")
83-
_, err := doh.LookupHost(context.Background(), "google.com")
84+
_, err := doh.LookupHost(context.Background(), HostLookup)
8485
if err != nil {
8586
t.FailNow()
8687
}

protocol/etch/engine.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@ import (
55
"log"
66
"net"
77
"net/http"
8+
"net/http/httputil"
9+
"net/url"
10+
11+
"github.com/mohanson/daze/lib/doa"
812
)
913

1014
// Server implemented the etch protocol.
1115
type Server struct {
1216
Closer io.Closer
1317
Listen string
18+
Server string
1419
}
1520

1621
// ServeHTTP implements http.Handler.
1722
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
18-
log.Println(w)
19-
log.Println(r)
23+
cloud := doa.Try(url.Parse(s.Server))
24+
proxy := httputil.NewSingleHostReverseProxy(cloud)
25+
proxy.Director = nil
26+
proxy.Rewrite = func(r *httputil.ProxyRequest) {
27+
r.SetURL(cloud)
28+
}
29+
proxy.ServeHTTP(w, r)
2030
}
2131

2232
// Close listener.
@@ -41,8 +51,10 @@ func (s *Server) Run() error {
4151
}
4252

4353
// NewServer returns a new Server.
44-
func NewServer(listen string) *Server {
54+
func NewServer(listen string, server string) *Server {
4555
return &Server{
56+
Closer: nil,
4657
Listen: listen,
58+
Server: server,
4759
}
4860
}

protocol/etch/engine_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package etch
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/mohanson/daze"
8+
"github.com/mohanson/daze/lib/doa"
9+
)
10+
11+
const (
12+
DazeServerListenOn = "127.0.0.1:28080"
13+
Dest = "https://1.1.1.1"
14+
HostLookup = "google.com"
15+
)
16+
17+
func TestProtocolEtch(t *testing.T) {
18+
server := NewServer(DazeServerListenOn, Dest)
19+
server.Run()
20+
defer server.Close()
21+
22+
client := daze.ResolverDoh("http://127.0.0.1:28080/dns-query")
23+
doa.Try(client.LookupHost(context.Background(), HostLookup))
24+
}

protocol/etch/etchmain/main.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)