海运的博客

GO HTTP client客户端使用

发布时间:January 6, 2015 // 分类:GO // No Comments

简单:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "strings"
)

func main() {
    res, err := http.Get("http://www.haiyun.me/")
    if err != nil {
        return
    }
        //执行close之前一定要判断错误,如没有body会崩溃
    defer res.Body.Close()
        //重用连接一定要执行上和下两步
    body, _ := ioutil.ReadAll(res.Body)
    fmt.Println(string(body))
        fmt.Println(res.Status)
    for k, v := range res.Header {
        fmt.Println(k, strings.Join(v, ""))
    }

}

自定义client,连接读写超时及自定义head:

package main

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

var timeout = time.Duration(20 * time.Second)

func dialTimeout(network, addr string) (net.Conn, error) {
        return net.DialTimeout(network, addr, timeout)
}

func main() {
        tr := &http.Transport{
                //使用带超时的连接函数
                Dial: dialTimeout,
                //建立连接后读超时
                ResponseHeaderTimeout: time.Second * 2,
        }
        client := &http.Client{
                Transport: tr,
                //总超时,包含连接读写
                Timeout: timeout,
        }
        req, _ := http.NewRequest("GET", "http://www.haiyun.me", nil)
        req.Header.Set("Connection", "keep-alive")
        res, err := client.Do(req)
        if err != nil {
                return
        }
        defer res.Body.Close()
        body, _ := ioutil.ReadAll(res.Body)
        fmt.Println(string(body))
        for k, v := range res.Header {
                fmt.Println(k, strings.Join(v, ""))
        }

}

使用代理及指定出口IP:

        //使用HTTP PROXY
        proxyUrl, err := url.Parse("http://host:port")
        tr := &http.Transport{
                Proxy: http.ProxyURL(proxyUrl),
        }
        //指定出口IP
        ief, err := net.InterfaceByName("eth0")
        addrs, err := ief.Addrs()
        addr := &net.TCPAddr{
                IP: addrs[0].(*net.IPNet).IP,
        }
        dia := net.Dialer{LocalAddr: addr}
        tr := &http.Transport{
                Dial: dia.Dial,
        }

使用socks5代理:https://github.com/hailiang/socks
http://golang.org/pkg/net/http/#Client
http://golang.org/pkg/net/http/#Transport
http://golang.org/pkg/net/#Dialer

自编译ngrok服务器

发布时间:August 18, 2014 // 分类:网络工具 // 15 Comments

首先安装GO环境,http://www.haiyun.me/archives/1009.html

cd /usr/local/src/
git clone https://github.com/inconshreveable/ngrok.git
export GOPATH=/usr/local/src/ngrok/
export NGROK_DOMAIN="haiyun.me"

生成自签名SSL证书,ngrok为ssl加密连接:

cd ngrok
openssl genrsa -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem
openssl genrsa -out device.key 2048
openssl req -new -key device.key -subj "/CN=$NGROK_DOMAIN" -out device.csr
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp device.crt assets/server/tls/snakeoil.crt 
cp device.key assets/server/tls/snakeoil.key
GOOS=linux GOARCH=386
make clean
make release-server release-client

如果一直停留在go get gopkg.in/yaml.v1参考:https://www.haiyun.me/archives/1011.html
启动SERVER:

bin/ngrokd -domain="$NGROK_DOMAIN" -httpAddr=":8000" 

交叉编译windows客户端,最好安装最新版本Golang,使用yum安装的一直编译不通过。

cd /usr/local/go/src/
GOOS=windows GOARCH=386 CGO_ENABLED=0 ./make.bash
cd -
GOOS=windows GOARCH=386 make release-server release-client

客户端配置:

server_addr: "haiyun.me:4443"
trust_host_root_certs: false
tunnels:
  http:
    subdomain: "example"
    auth: "user:12345"
    proto:
      http: "80"

  ssh:
    remote_port: 2222
    proto:
      tcp: "22"

启动客户端:

bin/ngrok -config ngrok.conf start http ssh

注意所有domain要一致,不然会出现证书错误:

Failed to read message: remote error: bad certificate

Windows/Centos安装GO语言环境

发布时间:August 18, 2014 // 分类:GO // 3 Comments

Centos下使用epel源安装:

yum install golang

Centos/Linux下源码安装golang:

wget https://dl.google.com/go/go1.13.4.linux-amd64.tar.gz
tar -C /usr/local -xzf go*linux-amd64.tar.gz
mkdir $HOME/go
echo 'export GOROOT=/usr/local/go' >> ~/.bashrc 
echo 'export GOPATH=$HOME/go' >> ~/.bashrc 
echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> ~/.bashrc 
source $HOME/.bashrc 

安装go get工具:

yum install mercurial git bzr subversion

Windows下安装:

https://dl.google.com/go/go1.11.2.windows-amd64.zip

设置环境变量:

setx GOOS windows
setx GOARCH amd64
setx GOROOT "D:\Program Files\go"
setx GOBIN "%GOROOT%\bin"
setx PATH %PATH%;"D:\Program Files\go\bin"

交叉编译:

CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

减小编译生成文件大小:

go build -ldflags "-s -w"
分类
最新文章
最近回复
  • mingyu wen: 写的让我豁然开朗,但请问这句什么意思:有状态的+无状态的,使用dhcpv6有状态分配地址,同时...
  • alex heryy: Androidblue
  • 海运: 不清楚,可尝试使用ubuntu18.04系统按本文方法编译试试。
  • bbr: 大佬,请问debian9编译错误怎么解决? github有人提问,没人解答,只好来这里看看.p...
  • 田博: 如果dts中没有USB信息,这样也能装上?
  • 海运: 正常情况下编译整个内核执行make menuconfig后就不会出现此提示,当单独编译单个模块...
  • oijq: 就是用的armbian的配置文件哈,按你的教程做的,在执行make LOCALVERSION=...
  • 海运: 使用armbian的配置文件,其它添加或修改自己懂的部分,不懂的就不要碰了。
  • oijq: 编译时这些选项全部选Y吗? Actions Semi Platforms (ARCH_ACTI...
  • 海运: n1编译bbr内核模块参考这个:https://www.haiyun.me/archives/...