Go language implements HTTP request forwarding

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("bytes" "Flag" "FMT" "io" "Log" "NET" "OS" "Strings" "Time") Func main () {var srchost, agencyhost Stringflag. Stringvar (&srchost, "Srchost", "" "," Srchost ") flag. Stringvar (&agencyhost, "Agencyhost", "" "," Agencyhost ") flag. Parse () if srchost = = "" | | Agencyhost = = "" {fmt. fprintf (OS. Stderr, "Fatal Error:%s", "parameter is empty") OS. Exit (1)}//establish socket, listen port Netlisten, err: = Net. Listen ("TCP", Srchost) checkerror (ERR) defer netlisten.close () Log ("Waiting for Clients") for {conn, err: = Netlisten.accept ()//If there is no request, always wait if err! = NIL {Continue}if Conn! = Nil {LOG (conn. Remoteaddr (). String (), "TCP Connect Success") Go Handleconnection (conn, agencyhost)//go can implement asynchronous concurrent request}}}//processing connection func handleconnection ( Conn Net. Conn, agencyhost string) {time. Sleep (time.millisecond) Buffer: = RECEIVEDATA (conn) if Len (buffer) > 1 {arr: = strings. Split (string (buffer), "\ r \ n") If Len (arr) > 1 {arr[1] = "Host:" + agencyhostnewstr: = Strings. Join (arr, "\ r \ n") sendagencyhost ([]byte (NEWSTR), Agencyhost, conn)}}conn. Close()}func sendagencyhost (data []byte, host string, Baseconn net. Conn) {Conn, _: = Net. Dial ("TCP", host) Conn. Write (data) time. Sleep (time.millisecond) Bufferhead: = Receivedata (conn) time. Sleep (time.millisecond) Bufferbody: = Receivedata (conn) var buf bytes. Bufferbuf.write (Bufferhead) buf. Write (bufferbody) baseconn. Write (BUF. Bytes ()) Conn. The Close ()}//receives the data unified method Func Receivedata (conn net. Conn) []byte {var buf bytes. Bufferbuffer: = Make ([]byte, 8192) for {sizenew, err: = conn. Read (buffer) buf. Write (Buffer[:sizenew]) If err = = Io. EOF | | Sizenew < 8192 {Break}}return buf. Bytes ()}//printing information Unified method Func log (v. ... interface{}) {log. Println (v ...)} Execute error Handling method func CheckError (err error) {if err! = Nil {fmt. fprintf (OS. Stderr, "Fatal Error:%s", err. Error ()) OS. Exit (1)}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.