Golang : Set up source IP address before making HTTP request

분류없음 2017.08.29 17:15


[출처] https://www.socketloop.com/tutorials/golang-set-up-source-ip-address-before-making-http-request


Problem :

There are times that you want to use certain IP address in a machine ( such as the one with permission to connect outside a firewall ) before making a HTTP request to another server. This is similar to the wget binding command :

For example : wget --bind-address=192.168.10.1 \

How to do that in Golang?

NOTE : This is not for masking or spoofing your IP address

Solution :

Build your own custom HTTP transport client and specify the local IP address that has the permission to connect outside a firewall.

For example :

 package main

 import (
         "fmt"
         "io/ioutil"
         "net"
         "net/http"
         "os"
         "time"
 )

 func main() {
         OKAddr := "<change here>" // local IP address to use

         OKAddress, _ := net.ResolveTCPAddr("tcp", OKAddr)

         transport := &http.Transport{
                 Proxy: http.ProxyFromEnvironment,
                 Dial: (&net.Dialer{
                         Timeout:   30 * time.Second,
                         KeepAlive: 30 * time.Second,
                         OKAddr: OKAddress}).Dial, TLSHandshakeTimeout: 10 * time.Second}

         client := &http.Client{
                 Transport: transport,
         }

         resp, err := client.Get("http://example.com/filetodown.zip")

         if err != nil {
                 panic(err)
         }

         defer resp.Body.Close()

         html, err := ioutil.ReadAll(resp.Body)

         if err != nil {
                 panic(err)
         }

         // fmt.Println(os.Stdout, string(html))
         // or save the zip - see https://www.socketloop.com/tutorials/golang-download-file-example
 }

References :

https://www.socketloop.com/tutorials/golang-convert-http-response-body-to-string

https://www.socketloop.com/tutorials/golang-download-file-example


신고
Trackback 0 : Comment 0

CURL ERROR 7 Failed to connect to Permission denied

APM 2017.07.25 08:25

“CURL ERROR 7 Failed to connect to Permission denied” error is caused, when for any reason curl request is blocked by some firewall or similar thing.

you will face this issue when ever the curl request is not with standard ports.

for example if you do curl to some URL which is on port 1234, you will face this issue where as URL with port 80 will give you results easily.

Most commonly this error has been seen on CentOS and any other OS with ‘SElinux’.

you need to either disable or change  ‘SElinux’ to permissive

1
nano /etc/selinux/config

locate following line

1
SELINUX=enforcing

Change this to

1
SELINUX=disabled

reboot is needed



SELINUX=disabled

출처: http://www.akashif.co.uk/php/curl-error-7-failed-to-connect-to-permission-denied



신고
Trackback 0 : Comment 0

커멘드 창에서 링크 만들기

분류없음 2016.12.20 12:06

1. 파일 심볼 링크 : mklink "링크" "원본"

2. 파일 하드 링크 : mklink /h "링크" "원본"

3. 폴더 심볼 링크 : mklink /d "링크" "원본"

4. 폴더 정션 링크 : mklink /j "링크" "원본"


http://cappleblog.co.kr/539

신고
Trackback 0 : Comment 0