Beispiel #1
0
func (p *Proxy) dns(page string, w http.ResponseWriter, r *http.Request) {
	f := p.profileOp.Open("localhost")
	if f == nil {
		fmt.Fprintln(w, "无效")
		return
	}

	r.ParseForm()
	if v, ok := r.Form["cmd"]; ok && len(v) > 0 {
		for _, cmd := range v {
			p.Command(cmd, f, nil)
		}
	}

	if len(page) == 0 || page == "/" {
		f.WriteDNS(w, p.serveIP)
	} else if _, m := httpd.MatchPath(page, "/export"); m {
		export := "# 此为 DNS 独立服务的配置导出,可复制所有内容至“命令”输入窗口重新加载此配置 #\n\n"
		export += "# Name: DNS 独立服务\n"
		export += f.ExportDNSCommand()
		export += "\n# end #\n"
		fmt.Fprintln(w, export)
	} else if target, m := httpd.MatchPath(page, "/history"); m {
		if len(target) > 0 {
			target = target[1:]
		}

		p.writeDNSHistory(w, p.lives.Open("localhost"), target)
	} else {
		http.Redirect(w, r, "..", 302)
	}
}
Beispiel #2
0
func (p *Proxy) OnRequest(
	w http.ResponseWriter,
	r *http.Request) {
	targetHost := httpd.RemoteHost(r.Host)
	remoteIP := httpd.RemoteHost(r.RemoteAddr)
	urlPath := r.URL.Path
	//fmt.Printf("host: %s/%s, remote: %s/%s, url: %s\n", targetHost, r.Host, remoteIP, r.RemoteAddr, urlPath)
	if strings.HasPrefix(urlPath, "http://") {
		p.proxyUrl(urlPath, w, r)
	} else if targetHost == "i.me" {
		p.initDevice(w, remoteIP)
	} else if !p.isSelfAddr(targetHost) && !p.isSelfAddr(remoteIP) {
		target := "http://" + r.Host + urlPath
		p.proxyUrl(target, w, r)
	} else if _, m := httpd.MatchPath(urlPath, "/post"); m {
		p.postTest(w, r)
	} else if page, m := httpd.MatchPath(urlPath, "/test/"); m {
		if page == "" {
			page = "localhost"
		}

		target := "http://" + page[1:]
		p.testUrl(target, w, r)
	} else if page, m := httpd.MatchPath(urlPath, "/to/"); m {
		target := "http://" + page[1:]
		p.proxyUrl(target, w, r)
	} else if _, m := httpd.MatchPath(urlPath, "/usage"); m {
		p.WriteUsage(w)
	} else if page, m := httpd.MatchPath(urlPath, "/profile"); m {
		p.ownProfile(remoteIP, page, w, r)
	} else if page, m := httpd.MatchPath(urlPath, "/dns"); m {
		if !p.disableDNS {
			p.dns(page, w, r)
		} else {
			fmt.Fprintln(w, "DNS is disabled")
		}
	} else if _, m := httpd.MatchPath(urlPath, "/res"); m {
		p.res(w, r, urlPath)
	} else if urlPath == "/" {
		p.index(w, p.ver)
	} else if urlPath == "/devices" {
		p.devices(w)
	} else if urlPath == "/about" {
		scheme := r.URL.Scheme
		if scheme == "" {
			scheme = "http"
		}

		fmt.Fprintf(w, "url%%v: %v\n", r.URL)
		fmt.Fprintln(w, "url.String(): "+r.URL.String())
		fmt.Fprintln(w)

		ex := ""
		if len(r.URL.RawQuery) > 0 {
			ex += "?" + r.URL.RawQuery
		}

		fmt.Fprintln(w, "url: "+r.Method+" "+scheme+"://"+r.Host+urlPath+ex)
		fmt.Fprintln(w, "remote: "+r.RemoteAddr)
		fmt.Fprintln(w, "requestURI: "+r.RequestURI)
		fmt.Fprintln(w, "host: "+r.Host)

		fmt.Fprintln(w)
		for k, v := range r.Header {
			fmt.Fprintln(w, "header: "+k+": "+strings.Join(v, "|"))
		}

		fmt.Fprintln(w)
		for _, s := range r.TransferEncoding {
			fmt.Fprintln(w, "transferEncoding: "+s)
		}

		fmt.Fprintln(w, "")
		fmt.Fprintln(w, "visit http://"+p.mainHost+"/about to get info")
		fmt.Fprintln(w, "visit http://"+p.mainHost+"/test/"+p.mainHost+"/about to test the proxy of http://"+p.mainHost+"/about")
		fmt.Fprintln(w, "visit http://"+p.mainHost+"/to/"+p.mainHost+"/about to purely proxy of http://"+p.mainHost+"/about")
		fmt.Fprintln(w, "")
		fmt.Fprintln(w, "visit http://"+p.mainHost+"/test/"+p.mainHost+"/test/"+p.mainHost+"/about to test the proxy")
	} else if urlPath == "/urlencoded" {
		p.urlEncoded(w)
	} else {
		fmt.Fprintln(w, "visit http://"+r.Host+"/about to get info")
	}
}