-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcasetornado.go
114 lines (93 loc) · 2.67 KB
/
testcasetornado.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strings"
)
var goTunnelLogger = log.New(os.Stdout, "[GoTunnel] ", log.Ltime|log.Ldate)
var msgLogger = log.New(os.Stdout, "[New Incoming Message] ", log.Ltime|log.Ldate|log.LstdFlags)
const subdomain string = "hidden-testcases-here"
type TunnelInfo struct {
Subdomain string `json:"id,omitempty"`
Port int `json:"port,omitempty"`
MaxConnCount int `json:"max_conn_count,omitempty"`
Url string `json:"url,omitempty"`
}
func main() {
handler := http.NewServeMux()
count := 0
handler.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi There %v\n", count)
msgLogger.Printf("Got a %v request from %v\n", r.Method, count)
count++
})
handler.HandleFunc("/api/post", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
var output strings.Builder
name := r.FormValue("name")
if name != "" {
output.WriteString("From:: " + name + " ")
}
meta := r.FormValue("meta")
if meta != "" {
output.WriteString("MetaData:: " + meta + " ")
}
file, fileHeader, err := r.FormFile("test_case")
if err == nil {
defer file.Close()
bytes, _ := io.ReadAll(file)
output.WriteString("\nFile Name:: " + fileHeader.Filename)
output.WriteString("\nSTART TESTCASE───────────────────────────────────────┐\n" +
string(bytes) +
"\n└───────────────────────────────────────END TESTCASE\n")
}
msgLogger.Print(output.String())
}
})
endPoint, err := net.Listen("tcp", "localhost:8080")
fatalErr(err)
server := http.Server{
Handler: handler,
}
go server.Serve(endPoint)
resp, err := http.Get("http://localtunnel.me/" + subdomain)
checkErr(err)
defer resp.Body.Close()
bytes, err := io.ReadAll(resp.Body)
checkErr(err)
info := TunnelInfo{}
json.Unmarshal(bytes, &info)
checkErr(err)
goTunnelLogger.Println("Tunnel URL :: " + info.Url)
// hack to tunnel localhost to a .loca.lt domain
fun := func() {
for {
remoteConn, err := net.Dial("tcp4", fmt.Sprintf("localtunnel.me:%d", info.Port))
fatalErr(err)
localConn, err := net.Dial("tcp4", fmt.Sprintf("localhost:%d", 8080))
fatalErr(err)
go io.Copy(remoteConn, localConn)
go io.Copy(localConn, remoteConn)
}
}
for i := 0; i < 9; i++ {
go fun()
}
fun()
}
func checkErr(error error) {
if error != nil {
goTunnelLogger.Println(error.Error())
}
}
func fatalErr(error error) {
if error != nil {
goTunnelLogger.Println(error.Error())
os.Exit(1)
}
}