Go語(yǔ)言實(shí)現(xiàn)單端口轉(zhuǎn)發(fā)到多個(gè)端口
1、背景
運(yùn)維和我提到這個(gè)需求的時(shí)候,我先是一愣,第一時(shí)間想到的是nginx,然后運(yùn)維說(shuō)nginx沒(méi)有這個(gè)功能……之所以選擇用go語(yǔ)言,主要兩點(diǎn):
- 靜態(tài)語(yǔ)言性能夠用
- 不用部署其它包,可以直接丟一個(gè)可執(zhí)行程序和一個(gè)配置上去就能運(yùn)行。
2、正題
這種東西,一看就肯定有前輩做過(guò),于是自行g(shù)ithub上找了一圈,果然有答案:
https://github.com/vulcand/oxy
于是用了,上述這個(gè)包,寫程序調(diào)用一下即可。
該包引用了其它開源的包哈(也可以用go mod哈,就不用自己去下載包啦~):
https://github.com/gorilla/websocket
https://github.com/mailgun/timetools
https://github.com/sirupsen/logrus
https://github.com/golang/sys
https://github.com/go-mgo/mgo/tree/v2
3、源碼
/**
* 2020 5 21 - sha zhen yu
*/
package main
import(
"log"
"strings"
"net/http"
"io/ioutil"
"encoding/json"
"github.com/vulcand/oxy/forward"
"github.com/vulcand/oxy/testutils"
)
type Rule struct{
Domain string `json:Domain`
Address string `json:Address`
}
type Setting struct{
MainPort string `json:MainPort`
Rules []Rule `json:Rules`
}
var setting Setting
var fwd *forward.Forwarder
func redirectHandle(w http.ResponseWriter, r *http.Request){
address := getAddress(r.Host)
if address != "" {
r.URL = testutils.ParseURI("http://"+address)
log.Println("Info: "+r.Host+" => "+address)
fwd.ServeHTTP(w, r)
} else {
w.WriteHeader(500)
}
}
func getAddress(host string) string {
for _,v := range setting.Rules {
if strings.Compare(host,v.Domain) == 0 {
return v.Address
}
}
return ""
}
func Init(){
log.Println("Init: start")
var Data,err = ioutil.ReadFile("setting.json")
if err != nil{
log.Fatal("Read Config File Error!")
return
}
err = json.Unmarshal(Data,&setting)
if err != nil{
log.Fatal("Read Config JSON Error!Please Check!")
return
}
log.Println("Main Port: "+setting.MainPort)
for i:=0;i<len(setting.Rules);i++{
log.Println("Import Rule: "+setting.Rules[i].Domain+" <----> "+setting.Rules[i].Address)
}
log.Println("Init: end")
}
func main(){
Init()
fwd, _ = forward.New()
redirect := http.HandlerFunc(redirectHandle)
s := &http.Server{
Addr: ":"+setting.MainPort,
Handler: redirect,
}
log.Println("Info: Listening port "+s.Addr)
s.ListenAndServe()
}
4、配置
{
"name": "HttpProxyForwarding",
"version": "1.0.0",
"MainPort": "80",
"Rules": [
{
"Domain":"test1.shazhenyu.com",
"Address":"193.207.242.129:81"
},
{
"Domain":"test2.shazhenyu.com",
"Address":"193.207.242.129:82"
}
]
}
5、效果

以上就是Go語(yǔ)言實(shí)現(xiàn)單端口轉(zhuǎn)發(fā)到多個(gè)端口的詳細(xì)內(nèi)容,更多關(guān)于Go單端口轉(zhuǎn)多端口的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何使用go實(shí)現(xiàn)創(chuàng)建WebSocket服務(wù)器
文章介紹了如何使用Go語(yǔ)言和gorilla/websocket庫(kù)創(chuàng)建一個(gè)簡(jiǎn)單的WebSocket服務(wù)器,并實(shí)現(xiàn)商品信息的實(shí)時(shí)廣播,感興趣的朋友一起看看吧2024-11-11
Golang標(biāo)準(zhǔn)庫(kù)unsafe源碼解讀
這篇文章主要為大家介紹了Golang標(biāo)準(zhǔn)庫(kù)unsafe源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Go語(yǔ)言實(shí)現(xiàn)AES加密并編寫一個(gè)命令行應(yīng)用程序
密碼學(xué)中的高級(jí)加密標(biāo)準(zhǔn)(Advanced Encryption Standard,AES),又稱Rijndael加密法,是經(jīng)常采用的一種區(qū)塊加密標(biāo)準(zhǔn)。本文就來(lái)用Go語(yǔ)言實(shí)現(xiàn)AES加密算法,需要的可以參考一下2023-02-02
golang實(shí)現(xiàn)簡(jiǎn)單rpc調(diào)用過(guò)程解析
這篇文章主要介紹了golang實(shí)現(xiàn)簡(jiǎn)單rpc調(diào)用,包括RPC具體實(shí)現(xiàn)結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

