Skip to content

Instantly share code, notes, and snippets.

@smartass08
Last active February 12, 2022 05:40
Show Gist options
  • Save smartass08/0f14ae3b1607e174e19c4688ad6d0cf0 to your computer and use it in GitHub Desktop.
Save smartass08/0f14ae3b1607e174e19c4688ad6d0cf0 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"time"
)
const cf string = `https://.+trycloudflare.com`
var link = ""
var wg sync.WaitGroup
func GetLink(w io.Writer, r io.Reader) {
defer wg.Done()
var wait = false
defer wg.Done()
match := regexp.MustCompile(cf)
var out []byte
buf := make([]byte, 1024, 1024)
for {
n, err := r.Read(buf[:])
if n > 0 {
d := buf[:n]
out = append(out, d...)
_, err := w.Write(d)
if err != nil {
return
}
}
if wait == false {
lines := strings.Split(string(out), "\n")
for _, s := range lines {
matches := match.FindAllString(s, -1)
for _, f := range matches {
if len(f) > 20 {
link = f
if match.MatchString(f) {
wait = true
}
}
}
}
}
if err != nil {
if err == io.EOF {
err = nil
}
}
}
}
func CF(cmd *exec.Cmd) {
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
err := cmd.Start()
if err != nil {
log.Fatalf("cmd.Start() failed with '%s'\n", err)
}
wg.Add(1)
go GetLink(os.Stdout, stdoutIn)
wg.Add(1)
go GetLink(os.Stderr, stderrIn)
}
func main() {
cmd := exec.Command("cloudflared", "tunnel", "--url", "localhost:9999", "--no-autoupdate")
go CF(cmd)
for len(link) == 0 {
time.Sleep(time.Second)
}
for {
time.Sleep(time.Second)
fmt.Println(link)
}
}
@smartass08
Copy link
Author

Wait for the link to get generated and use the variable link as needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment