Last active
February 25, 2019 15:35
-
-
Save Attumm/79eb6b4cb33145b91a4aade26e7b35cb to your computer and use it in GitHub Desktop.
proxy golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "flag" | |
| "fmt" | |
| ) | |
| func main() { | |
| paths := map[string] func () { | |
| "proxy": main_proxy_server, | |
| "mirror": main_mirror_server, | |
| "resend": main_resend_request, | |
| } | |
| var command string | |
| flag.StringVar(&command, "command", "proxy", "Run 'proxy, resend, mirror', by specifing what to run") | |
| flag.Parse() | |
| fmt.Println("started", command) | |
| paths[command]() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| type handledReq struct { | |
| Url string | |
| Headers map[string][]string | |
| Body string | |
| Form map[string][]string | |
| Method string | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "net/http" | |
| "net/url" | |
| "log" | |
| "io/ioutil" | |
| "strings" | |
| "encoding/json" | |
| "os" | |
| ) | |
| func reqHandler(chanhandleReq chan *handledReq) func(http.ResponseWriter, *http.Request) { | |
| return func(w http.ResponseWriter, r *http.Request) { | |
| fmt.Println("------------Start---------------") | |
| client := &http.Client{} | |
| data := url.Values{} | |
| req, err := http.NewRequest(r.Method, r.URL.String(), nil) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| body, err := ioutil.ReadAll(r.Body) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| //if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { | |
| r.ParseForm() | |
| if len(r.Form) > 0 { | |
| for k, v := range r.Form { | |
| data.Set(k, strings.Join(v, ", ")) | |
| } | |
| req, _ = http.NewRequest(r.Method, r.URL.String(), strings.NewReader(data.Encode())) | |
| } | |
| if len(body) > 0 { | |
| req, _ = http.NewRequest(r.Method, r.URL.String(), strings.NewReader(string(body))) | |
| } | |
| if r.Header.Get("Content-Type") == "multipart/form-data" { | |
| fmt.Println("multiform not handled") | |
| } | |
| for k, v := range r.Header { | |
| fmt.Println(k, v) | |
| req.Header.Add(k, strings.Join(v, ", ")) | |
| } | |
| hold := &handledReq{ | |
| Url: r.URL.String(), | |
| Headers: r.Header, | |
| Body: string(body), | |
| Form: r.Form, | |
| Method: r.Method, | |
| } | |
| resp, err := client.Do(req) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer resp.Body.Close() | |
| resPbody, err := ioutil.ReadAll(resp.Body) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Fprintf(w, string(resPbody)) | |
| fmt.Println("------------End---------------") | |
| chanhandleReq <- hold | |
| } | |
| } | |
| func storeTofile(chanhandleReq chan *handledReq) { | |
| f, err := os.OpenFile("/tmp/hoi", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer f.Close() | |
| for item := range chanhandleReq { | |
| j, _ := json.Marshal(&item) | |
| if _, err = f.WriteString(string(j)+"\n"); err != nil { | |
| panic(err) | |
| } | |
| } | |
| } | |
| func main_proxy_server() { | |
| chanhandleReq := make(chan *handledReq, 300) | |
| go storeTofile(chanhandleReq) | |
| reqHandlerWithChan := reqHandler(chanhandleReq) | |
| http.HandleFunc("/", reqHandlerWithChan) | |
| log.Fatal(http.ListenAndServe(":8000", nil)) | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "net/http" | |
| "log" | |
| "io/ioutil" | |
| ) | |
| func postHandler(w http.ResponseWriter, r *http.Request) { | |
| r.ParseForm() | |
| for k, v := range r.Form { | |
| fmt.Fprint(w, k, v, "\n") | |
| fmt.Print("key", k, "val", v) | |
| } | |
| fmt.Println(r.URL.String()) | |
| fmt.Fprintf(w, "Konichiwa b\r\n") | |
| body, err := ioutil.ReadAll(r.Body) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Println(string(body)) | |
| } | |
| func main_mirror_server() { | |
| http.HandleFunc("/", postHandler) | |
| log.Fatal(http.ListenAndServe(":8888", nil)) | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "net/http" | |
| "net/url" | |
| "fmt" | |
| "bufio" | |
| "os" | |
| "encoding/json" | |
| "strings" | |
| "io/ioutil" | |
| "sync" | |
| ) | |
| var wg sync.WaitGroup | |
| func sendWorker(i int, chanhandleReq chan string) { | |
| for s := range chanhandleReq { | |
| var hold handledReq | |
| err := json.Unmarshal([]byte(s), &hold) | |
| if err != nil { | |
| fmt.Println(err) | |
| } | |
| client := &http.Client{ | |
| // CheckRedirect: redirectPolicyFunc, | |
| } | |
| data := url.Values{} | |
| req, err := http.NewRequest(hold.Method, hold.Url, nil) | |
| if err != nil { | |
| fmt.Println(err) | |
| } | |
| if len(hold.Form) > 0 { | |
| for k, v := range hold.Form { | |
| fmt.Println("form", "k", k, "v", v) | |
| data.Set(k, strings.Join(v, ", ")) | |
| } | |
| req, _ = http.NewRequest(hold.Method, hold.Url, strings.NewReader(data.Encode())) | |
| } | |
| if len(hold.Body) > 0 { | |
| req, _ = http.NewRequest(hold.Method, hold.Url, strings.NewReader(hold.Body)) | |
| } | |
| if strings.Join(hold.Form["Content-Type"], ", ") == "multipart/form-data" { | |
| fmt.Println("multiform not handled") | |
| } | |
| for k, v := range hold.Headers { | |
| req.Header.Add(k, strings.Join(v, ", ")) | |
| } | |
| resp, err := client.Do(req) | |
| if err != nil { | |
| fmt.Println(err) | |
| } | |
| defer resp.Body.Close() | |
| body, err := ioutil.ReadAll(resp.Body) | |
| fmt.Println(string(body)) | |
| if err != nil { | |
| fmt.Println(err) | |
| } | |
| } | |
| fmt.Println(i, "worker done") | |
| wg.Done() | |
| } | |
| func main_resend_request() { | |
| workers := 1 | |
| wg.Add(workers) | |
| chanhandleReq := make(chan string, 1000) | |
| for i:=0; i<workers; i++ { | |
| go sendWorker(i, chanhandleReq) | |
| } | |
| file, err := os.Open("/tmp/hoi") | |
| if err != nil { | |
| fmt.Println(err) | |
| } | |
| defer file.Close() | |
| scanner := bufio.NewScanner(file) | |
| count := 0 | |
| for scanner.Scan() { | |
| count++ | |
| if err := scanner.Err(); err != nil { | |
| fmt.Println(err) | |
| } | |
| chanhandleReq <- scanner.Text() | |
| fmt.Println("send", count) | |
| } | |
| fmt.Println("STOP") | |
| close(chanhandleReq) | |
| wg.Wait() | |
| fmt.Println("send", count) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment