Golang tcp 2 unix socket proxy, golangproxy

Source: Internet
Author: User

Golang tcp 2 unix socket proxy, golangproxy

To block mysql TCP, all external links are controlled by my proxy. so writes a tcp-to-unix socket proxy.


package mainimport ("os""fmt""net""io""sync""time")type proxy struct{Host stringPort stringLocal string}func runProxy(list []proxy) {wg := sync.WaitGroup{}for _, v := range list {wg.Add(1)go func() {eachServer(v.Host, v.Port, v.Local)wg.Done()}()}wg.Wait()}func eachServer(host string, port string, local string) {l,err := net.Listen("tcp", fmt.Sprintf("%s:%s", host, port))if err != nil {fmt.Print("listen tcp :%s",err.Error())os.Exit(1)}defer l.Close()for {tc,err := l.Accept()if err != nil {fmt.Printf("accept tcp conn :%s",err.Error())tc.Close()continue}go eachConn(local, tc)}}func eachConn(local string, tc net.Conn) {uc,err := net.Dial("unix", local)if err != nil {fmt.Printf("get unix conn :%s",err.Error())uc.Close()return}go io.Copy(tc, uc)go io.Copy(uc, tc)}func main() {list := []proxy{proxy{Host:"192.168.8.101",Port:"3306",Local:"/home/work/data/tmp/mysql3306.sock"}}runProxy(list)}



How does golang write two loop variables into one?

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.