Skip to content

Commit

Permalink
Add DoH Config (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna committed May 1, 2024
1 parent 2318bba commit 6361b97
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
4 changes: 4 additions & 0 deletions x/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func NewDefaultConfigParser() *ConfigParser {
p := new(ConfigParser)

// Please keep the list in alphabetical order.
p.RegisterStreamDialerWrapper("doh", wrapStreamDialerWithDOH)
p.RegisterPacketDialerWrapper("doh", func(baseDialer transport.PacketDialer, wrapConfig *url.URL) (transport.PacketDialer, error) {
return nil, errors.New("doh is not supported for PacketDialers")
})

p.RegisterStreamDialerWrapper("override", wrapStreamDialerWithOverride)
p.RegisterPacketDialerWrapper("override", wrapPacketDialerWithOverride)
Expand Down
66 changes: 66 additions & 0 deletions x/config/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2024 Jigsaw Operations LLC
//
// 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
//
// https://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 config

import (
"errors"
"fmt"
"net"
"net/url"
"strings"

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

func wrapStreamDialerWithDOH(innerDialer transport.StreamDialer, configURL *url.URL) (transport.StreamDialer, error) {
query := configURL.Opaque
values, err := url.ParseQuery(query)
if err != nil {
return nil, err
}
var name, address string
for key, values := range values {
switch strings.ToLower(key) {
case "address":
if len(values) != 1 {
return nil, fmt.Errorf("address option must has one value, found %v", len(values))
}
address = values[0]
case "name":
if len(values) != 1 {
return nil, fmt.Errorf("name option must has one value, found %v", len(values))
}
name = values[0]
default:
return nil, fmt.Errorf("unsupported option %v", key)

}
}
if name == "" {
return nil, errors.New("must set a name")
}
if address == "" {
address = name
}
_, port, err := net.SplitHostPort(address)
if err != nil {
address = net.JoinHostPort(address, "443")
port = "443"
}
dohURL := url.URL{Scheme: "https", Host: net.JoinHostPort(name, port), Path: "/dns-query"}
resolver := dns.NewHTTPSResolver(innerDialer, address, dohURL.String())
return dns.NewStreamDialer(resolver, innerDialer)
}
8 changes: 8 additions & 0 deletions x/config/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ SOCKS5 proxy (currently streams only, package [github.com/Jigsaw-Code/outline-sd
USERINFO field is optional and only required if username and password authentication is used. It is in the format of username:password.
DNS-over-HTTPS resolution (streams only, package [github.com/Jigsaw-Code/outline-sdk/dns])
It takes a host name and a host:port address. The name will be used in the SNI and Host header, while the address is used to connect
to the DoH server. The address is optional, and will default to "[NAME]:443". The resulting dialer will use the input dialer with
Happy Eyeballs to connect to the destination.
doh:name=[NAME]&address=[ADDRESS]
Stream split transport (streams only, package [github.com/Jigsaw-Code/outline-sdk/transport/split])
It takes the length of the prefix. The stream will be split when PREFIX_LENGTH bytes are first written.
Expand Down
3 changes: 2 additions & 1 deletion x/examples/fetch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {
transportFlag := flag.String("transport", "", "Transport config")
addressFlag := flag.String("address", "", "Address to connect to. If empty, use the URL authority")
methodFlag := flag.String("method", "GET", "The HTTP method to use")
timeoutSecFlag := flag.Int("timeout", 5, "Timeout in seconds")

flag.Parse()

Expand Down Expand Up @@ -89,7 +90,7 @@ func main() {
}
return dialer.DialStream(ctx, net.JoinHostPort(host, port))
}
httpClient := &http.Client{Transport: &http.Transport{DialContext: dialContext}, Timeout: 5 * time.Second}
httpClient := &http.Client{Transport: &http.Transport{DialContext: dialContext}, Timeout: time.Duration(*timeoutSecFlag) * time.Second}

req, err := http.NewRequest(*methodFlag, url, nil)
if err != nil {
Expand Down

0 comments on commit 6361b97

Please sign in to comment.