diff --git a/example/introducer-fetch/main.go b/example/introducer-fetch/main.go new file mode 100644 index 0000000000000000000000000000000000000000..d37a551a0b1c07874c4c68aa433c0f065e553a4a --- /dev/null +++ b/example/introducer-fetch/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "flag" + "fmt" + "io" + "log" + "log/slog" + "os" + "time" + + "0xacab.org/leap/bitmask-core/pkg/introducer" +) + +func main() { + introducerIP := flag.String("introducer-ip", "", "IP of introducer to fetch through") + introducerPort := flag.String("introducer-port", "", "Port of introducer to fetch through") + obfs4Cert := flag.String("obfs4-cert", "", "OBFS4 Certificate") + kcp := flag.Bool("kcp", false, "Whether to attempt to use KCP") + verbose := flag.Bool("verbose", false, "Whether to log at DEBUG level") + url := flag.String("url", "", "URL to fetch w/ the introducer") + + flag.Parse() + if *introducerIP == "" || *introducerPort == "" || *obfs4Cert == "" || *url == "" { + flag.Usage() + os.Exit(1) + } + + level := slog.LevelInfo + if *verbose { + level = slog.LevelDebug + } + + logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + Level: level, + })) + slog.SetDefault(logger) + + intro := &introducer.Introducer{ + Type: "obfsvpnintro", + Addr: fmt.Sprintf("%s:%s", *introducerIP, *introducerPort), + Cert: *obfs4Cert, + FQDN: "example.com", + KCP: *kcp, + } + + client, err := introducer.NewHTTPClientFromIntroducer(intro) + if err != nil { + log.Fatalf("Error getting http client from introducer: %v", err) + } + + client.Timeout = 5 * time.Second + + resp, err := client.Get(*url) + if err != nil { + log.Fatalf("Error GET-ing url: %v", err) + } + + if resp.StatusCode < 200 || resp.StatusCode >= 400 { + log.Fatalf("Got a non-success response status code: %+v", resp) + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatalf("Error reading response body: %v", err) + } + + logger.Info("Body of response:\n", body) +} diff --git a/pkg/introducer/httpClient.go b/pkg/introducer/httpClient.go index 877d353f686c65410c1cabf0d5939adeeddb6745..ad187aab013892da98003ad6fe7da21cf394fcf6 100644 --- a/pkg/introducer/httpClient.go +++ b/pkg/introducer/httpClient.go @@ -43,6 +43,7 @@ func NewHTTPClientFromIntroducer(introducer *Introducer) (*http.Client, error) { } switch { + // TODO: use obfsvpn.GetKCPDialer case introducer.KCP: dialer.DialFunc = func(network, address string) (net.Conn, error) { log.Debug().Msg(fmt.Sprintf("dialing kcp://%s", address))