go 代理服务器(cookie设置失败无法登录),nginx设置Domain
1 Method POST 2 URL https://vjudge.net/user/login 3 Proto HTTP/1.1 4 ProtoMajor 1 5 ProtoMinor 1 6 Header map[Referer:[http://127.0.0.1:8002/] Accept-Language:[zh-CN,zh;q=0.8] Connection:[keep-alive] Origin:[http://127.0.0.1:8002] Content-Type:[application/x-www-form-urlencoded; charset=UTF-8] X-Requested-With:[XMLHttpRequest] Accept-Encoding:[gzip, deflate, br] Cookie:[io=oiuZ1YssLxHIpLGMAAAo; _ga=GA1.1.1822669068.1495898591; _gid=GA1.1.2058057953.1495904201] Content-Length:[39] User-Agent:[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36] Accept:[*/*]] 7 Body &{0xc42011afe0 <nil> <nil> false true {0 0} false false false 0x6068c0} 8 ContentLength 39 9 TransferEncoding [] 10 Close false 11 Host vjudge.net 12 Form map[] 13 PostForm 0x6114c0 14 MultipartForm <nil> 15 Trailer map[] 16 RemoteAddr 127.0.0.1:37558 17 RequestURI 18 TLS <nil> 19 Cancel <nil> 20 Response <nil> 21 -------------------------------------------------------------- 22 Status 200 OK 23 StatusCode 200 24 Proto HTTP/2.0 25 ProtoMajor 2 26 ProtoMinor 0 27 Header map[Server:[nginx/1.10.3] Date:[Sat, 27 May 2017 16:56:53 GMT] Content-Type:[text/plain;charset=ISO-8859-1] Content-Length:[7] Set-Cookie:[JSESSIONID=F55D0140082CA33C63F7A7C7B4C968B9; Domain=.vjudge.net; Path=/; HttpOnly Jax.Q=1406100039|89TFHFKYFR2DTH8AVLC2JRLIUTMJLE; Domain=.vjudge.net; Expires=Sun, 27-May-2018 16:56:53 GMT; Path=/] Access-Control-Allow-Origin:[http://127.0.0.1:8002] Vary:[Origin] Access-Control-Allow-Credentials:[true]] 28 Body {0xc420405a40} 29 ContentLength 7 30 TransferEncoding [] 31 Close false 32 Uncompressed false 33 Trailer map[] 34 Request &{POST https://vjudge.net/user/login HTTP/1.1 1 1 map[Connection:[keep-alive] Origin:[http://127.0.0.1:8002] Content-Type:[application/x-www-form-urlencoded; charset=UTF-8] X-Requested-With:[XMLHttpRequest] Referer:[http://127.0.0.1:8002/] Accept-Language:[zh-CN,zh;q=0.8] Content-Length:[39] User-Agent:[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36] Accept:[*/*] Accept-Encoding:[gzip, deflate, br] Cookie:[io=oiuZ1YssLxHIpLGMAAAo; _ga=GA1.1.1822669068.1495898591; _gid=GA1.1.2058057953.1495904201]] 0xc4200e6740 <nil> 39 [] false vjudge.net map[] map[] <nil> map[] 127.0.0.1:37558 <nil> <nil> <nil> 0xc4200e6780} 35 TLS &{771 true false 49200 h2 true [0xc420152000 0xc420152480] [[0xc420152000 0xc420152480 0xc420252900]] [] [] [23 179 124 44 29 163 172 94 255 255 155 248]} 36 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
设置cookie失败是因为Set-Cookie的Domain=.vjudge.net; 改为自己的域名“.gzhuacm.cn"或者”“空字符串就可以了,有这个的话浏览器会根据域名设置为指定域名的cookie
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type route struct {
}
func printReq(req *http.Request) {
fmt.Println("Method", req.Method)
fmt.Println("URL", req.URL)
fmt.Println("Proto", req.Proto)
fmt.Println("ProtoMajor", req.ProtoMajor)
fmt.Println("ProtoMinor", req.ProtoMinor)
fmt.Println("Header", req.Header)
fmt.Println("Body", req.Body)
fmt.Println("ContentLength", req.ContentLength)
fmt.Println("TransferEncoding", req.TransferEncoding)
fmt.Println("Close", req.Close)
fmt.Println("Host", req.Host)
fmt.Println("Form", req.Form)
fmt.Println("PostForm", req.PostForm)
fmt.Println("MultipartForm", req.MultipartForm)
fmt.Println("Trailer", req.Trailer)
fmt.Println("RemoteAddr", req.RemoteAddr)
fmt.Println("RequestURI", req.RequestURI)
fmt.Println("TLS", req.TLS)
fmt.Println("Cancel", req.Cancel)
fmt.Println("Response", req.Response)
fmt.Println("--------------------------------------------------------------")
}
func printRes(res *http.Response) {
fmt.Println("Status", res.Status)
fmt.Println("StatusCode", res.StatusCode)
fmt.Println("Proto", res.Proto)
fmt.Println("ProtoMajor", res.ProtoMajor)
fmt.Println("ProtoMinor", res.ProtoMinor)
fmt.Println("Header", res.Header)
fmt.Println("Body", res.Body)
fmt.Println("ContentLength", res.ContentLength)
fmt.Println("TransferEncoding", res.TransferEncoding)
fmt.Println("Close", res.Close)
fmt.Println("Uncompressed", res.Uncompressed)
fmt.Println("Trailer", res.Trailer)
fmt.Println("Request", res.Request)
fmt.Println("TLS", res.TLS)
fmt.Println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
}
func (r route) ServeHTTP(w http.ResponseWriter, req *http.Request) {
//req.URL.Host = "https://vjude.net"
printReq(req)
path := "https://vjudge.net"
originDomain := "Domain=.vjudge.net;"
domain := "Domain=.vjudge.gzhuacm.cn;"
//u, err := url.Parse("https://vjudge.net")
u, err := url.Parse(path + req.RequestURI)
if err != nil {
fmt.Println(err)
return
}
//fmt.Println("url:", u.RawPath, u.Host, u.RawQuery)
client0 := &http.Client{}
//req1, err := http.NewRequest("GET", path, nil)
//printReq(req1)
req.URL = u
req.RequestURI = ""
req.Host = u.Host
printReq(req)
//req1, err := http.NewRequest(req.Method, path+req.RequestURI, req.Body)
if err != nil {
fmt.Println(err)
return
}
resp, err := client0.Do(req)
if err != nil {
fmt.Println(err)
return
}
printRes(resp)
defer resp.Body.Close()
for k, v := range resp.Header {
for _, vv := range v {
if strings.Count(vv, originDomain)!=0 {
vv = strings.Replace(vv, originDomain, domain, -1)
}
w.Header().Add(k, vv)
}
}
fmt.Println(w.Header(),"\n----------------------\n");
w.WriteHeader(resp.StatusCode)
result, err := ioutil.ReadAll(resp.Body)
if err != nil && err != io.EOF {
panic(err)
}
w.Write(result)
}
func main() {
r := &route{}
http.ListenAndServe(":8002", r)
}
然后用nginx监听vjudge.gzhuacm.cn,转发到本地8002端口
如果用内网的话setsid autossh -CNg -L 9000:127.0.0.1:8002 root@gzhuacm.cn 将本地9000端口的访问转发到远程的8002端口处理(两层代理),访问内网的ip:9000 ,用两层代理是为了实现网上断网后也可以通过校园网访问oj
后面去nginx官网差了下,原来可以直接在nginx改,这样就不用go转发了
| Syntax: | proxy_cookie_domain proxy_cookie_domain |
|---|---|
| Default: |
proxy_cookie_domain off; |
| Context: | http, server, location |
然后改成proxy_cookie_domain .vjudge.net vj.gzhuacm.cn

浙公网安备 33010602011771号