Skip to content

Commit

Permalink
change dns server
Browse files Browse the repository at this point in the history
  • Loading branch information
ilanyu committed Apr 16, 2018
1 parent 6d87af2 commit 616cf11
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import "flag"
type Cmd struct {
bind string
remote string
ip string
}

func parseCmd() Cmd {
var cmd Cmd
flag.StringVar(&cmd.bind, "l", "0.0.0.0:8888", "listen on ip:port")
flag.StringVar(&cmd.remote, "r", "http://idea.lanyus.com:80", "reverse proxy addr")
flag.StringVar(&cmd.ip, "ip", "", "reverse proxy addr server ip")
flag.Parse()
return cmd
}
19 changes: 9 additions & 10 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"log"
"net"
"time"
"io/ioutil"
"context"
"github.com/bogdanovich/dns_resolver"
"strings"
)

type handle struct {
Expand All @@ -27,19 +28,17 @@ func (this *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
DualStack: true,
}
http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
if addr == "idea.lanyus.com:80" {
resp, err := http.Get("http://119.29.29.29/d?dn=idea.lanyus.com")
remote := strings.Split(addr, ":")
if cmd.ip == "" {
resolver := dns_resolver.New([]string{"114.114.114.114", "114.114.115.115", "119.29.29.29", "223.5.5.5", "8.8.8.8", "208.67.222.222", "208.67.220.220"})
resolver.RetryTimes = 5
ip, err := resolver.LookupHost(remote[0])
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
res, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
addr = string(res) + ":80"
cmd.ip = ip[0].String()
}

addr = cmd.ip + ":" + remote[1]
return dialer.DialContext(ctx, network, addr)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
)

var cmd Cmd
var srv http.Server

func StartServer(bind string, remote string) {
Expand All @@ -26,6 +27,6 @@ func StopServer() {
}

func main() {
cmd := parseCmd()
cmd = parseCmd()
StartServer(cmd.bind, cmd.remote)
}

0 comments on commit 616cf11

Please sign in to comment.