Skip to content

Commit 9d45e58

Browse files
yagilbradfitz
authored andcommitted
Add tailscale_enable_funnel_to_localhost_plaintext_http1 to tailscale.h
1 parent 75f9bc2 commit 9d45e58

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

tailscale.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern int TsnetGetIps(int sd, char *buf, size_t buflen);
2323
extern int TsnetGetRemoteAddr(int listener, int conn, char *buf, size_t buflen);
2424
extern int TsnetListen(int sd, char* net, char* addr, int* listenerOut);
2525
extern int TsnetLoopback(int sd, char* addrOut, size_t addrLen, char* proxyOut, char* localOut);
26+
extern int TsnetEnableFunnelToLocalhostPlaintextHttp1(int sd, int localhostPort);
2627

2728
tailscale tailscale_new() {
2829
return TsnetNewServer();
@@ -106,3 +107,7 @@ int tailscale_loopback(tailscale sd, char* addr_out, size_t addrlen, char* proxy
106107
int tailscale_errmsg(tailscale sd, char* buf, size_t buflen) {
107108
return TsnetErrmsg(sd, buf, buflen);
108109
}
110+
111+
int tailscale_enable_funnel_to_localhost_plaintext_http1(tailscale sd, int localhostPort) {
112+
return TsnetEnableFunnelToLocalhostPlaintextHttp1(sd, localhostPort);
113+
}

tailscale.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import (
1414
"net"
1515
"os"
1616
"regexp"
17+
"strconv"
1718
"strings"
1819
"sync"
1920
"syscall"
2021
"unsafe"
2122

2223
"tailscale.com/hostinfo"
24+
"tailscale.com/ipn"
2325
"tailscale.com/tsnet"
2426
"tailscale.com/types/logger"
2527
)
@@ -531,3 +533,44 @@ func TsnetLoopback(sd C.int, addrOut *C.char, addrLen C.size_t, proxyOut *C.char
531533

532534
return 0
533535
}
536+
537+
//export TsnetEnableFunnelToLocalhostPlaintextHttp1
538+
func TsnetEnableFunnelToLocalhostPlaintextHttp1(sd C.int, localhostPort C.int) C.int {
539+
s, err := getServer(sd)
540+
if err != nil {
541+
return s.recErr(err)
542+
}
543+
544+
ctx := context.Background()
545+
lc, err := s.s.LocalClient()
546+
if err != nil {
547+
return s.recErr(err)
548+
}
549+
550+
st, err := lc.StatusWithoutPeers(ctx)
551+
if err != nil {
552+
return s.recErr(err)
553+
}
554+
domain := st.CertDomains[0]
555+
556+
hp := ipn.HostPort(net.JoinHostPort(domain, strconv.Itoa(443)))
557+
tcpForward := fmt.Sprintf("127.0.0.1:%d", localhostPort)
558+
sc := &ipn.ServeConfig{
559+
TCP: map[uint16]*ipn.TCPPortHandler{
560+
443: {
561+
TCPForward: tcpForward,
562+
TerminateTLS: domain,
563+
},
564+
},
565+
AllowFunnel: map[ipn.HostPort]bool{
566+
hp: true,
567+
},
568+
}
569+
570+
lc.SetServeConfig(ctx, sc)
571+
if !sc.AllowFunnel[hp] {
572+
return s.recErr(fmt.Errorf("libtailscale: failed to enable funnel"))
573+
}
574+
575+
return 0
576+
}

tailscale.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@ extern int tailscale_accept(tailscale_listener listener, tailscale_conn* conn_ou
175175
// Returns zero on success or -1 on error, call tailscale_errmsg for details.
176176
extern int tailscale_loopback(tailscale sd, char* addr_out, size_t addrlen, char* proxy_cred_out, char* local_api_cred_out);
177177

178+
// tailscale_enable_funnel_to_localhost_plaintext_http1 configures sd to have
179+
// Tailscale Funnel enabled, routing requests from the public web
180+
// (without any authentication) down to this Tailscale node, requesting new
181+
// LetsEncrypt TLS certs as needed, terminating TLS, and proxying all incoming
182+
// HTTPS requests to http://127.0.0.1:localhostPort without TLS.
183+
//
184+
// There should be a plaintext HTTP/1 server listening on 127.0.0.1:localhostPort
185+
// or tsnet will serve HTTP 502 errors.
186+
//
187+
// Expect junk traffic from the internet from bots watching the public CT logs.
188+
//
189+
// Returns:
190+
// 0 - success
191+
// -1 - other error, details printed to the tsnet logger
192+
extern int tailscale_enable_funnel_to_localhost_plaintext_http1(tailscale sd, int localhostPort);
193+
178194
// tailscale_errmsg writes the details of the last error to buf.
179195
//
180196
// After returning, buf is always NUL-terminated.

0 commit comments

Comments
 (0)