-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (114 loc) · 2.79 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
)
var ports map[string]string
func main() {
profile := ""
if len(os.Args) > 1 {
profile = os.Args[1]
fmt.Println("Profile " + profile)
}
readPorts(profile)
serverMuxA := http.NewServeMux()
serverMuxA.HandleFunc("/", defaultHandler)
serverMuxB := http.NewServeMux()
serverMuxB.HandleFunc("/", handler)
fmt.Println("Starting Proxy Service ...")
for key, val := range ports {
fmt.Println("port " + key + " redirect to "+ val)
go http.ListenAndServe(":"+key, serverMuxB)
}
http.ListenAndServe(":1000", serverMuxA)
}
func readPorts(profile string){
fileName := "ports.txt"
if len(strings.TrimSpace(profile)) > 0 {
fileName = "ports-"+profile+".txt"
if !fileExists(fileName) {
fmt.Println("File " + fileName + " not exist")
fmt.Println("Open default file ports.txt")
fileName = "ports.txt"
}
}
file, err := os.Open(fileName)
fatal(err)
defer file.Close()
scanner := bufio.NewScanner(file)
ports = map[string]string{}
for scanner.Scan() {
lineStr := scanner.Text()
if len(strings.TrimSpace(lineStr)) == 0{
continue
}
mapPort := strings.Split(lineStr, ";")
ports[mapPort[0]] = mapPort[1]
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func handler(w http.ResponseWriter, r *http.Request){
localAddr := r.Context().Value(http.LocalAddrContextKey).(*net.TCPAddr)
port := strconv.Itoa(localAddr.Port)
targetUrl := ports[port]
fmt.Println("HOST : " + r.Host)
fmt.Println("PORT : " + port)
fmt.Println("URL : " + targetUrl)
uri := targetUrl+r.RequestURI
fmt.Println(r.Method + ": " + uri)
var jsonStr = []byte("")
if r.Method == http.MethodPost || r.Method == http.MethodPut {
body, err := ioutil.ReadAll(r.Body)
fatal(err)
fmt.Printf("Body: %v\n", string(body))
jsonStr = []byte(body)
}
rr, err := http.NewRequest(r.Method, uri, bytes.NewBuffer(jsonStr))
fatal(err)
copyHeader(r.Header, &rr.Header)
var transport http.Transport
resp, err := transport.RoundTrip(rr)
fatal(err)
fmt.Printf("Resp-Headers: %v\n", resp.Header)
fmt.Println("====================================================================")
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fatal(err)
dH := w.Header()
copyHeader(resp.Header, &dH)
dH.Add("Requested-Host", rr.Host)
w.Write(body)
}
func defaultHandler(w http.ResponseWriter, r *http.Request){
fmt.Fprint(w, "Default Value")
}
func fatal(err error) {
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}
func copyHeader(source http.Header, dest *http.Header){
for n, v := range source {
for _, vv := range v {
dest.Add(n, vv)
}
}
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}