Comments (4)
Sorry for the message, I just found your group on Gitter and Telegram
from goproxy.
your purpose is easily achieved by golang,the fllowing is an example for you,
package main
import (
"fmt"
"io"
"net/http"
"time"
)
var verbose = false;
var passthruRequestHeaderKeys = [...]string{
"Accept",
"Accept-Encoding",
"Accept-Language",
"Cache-Control",
"Cookie",
"Referer",
"User-Agent",
}
var passthruResponseHeaderKeys = [...]string{
"Content-Encoding",
"Content-Language",
"Content-Type",
"Cache-Control", // TODO: Is this valid in a response?
"Date",
"Etag",
"Expires",
"Last-Modified",
"Location",
"Server",
"Vary",
}
func main() {
handler := http.DefaultServeMux
handler.HandleFunc("/", handleFunc)
s := &http.Server{
Addr: ":8080",
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}
func handleFunc(w http.ResponseWriter, r *http.Request) {
fmt.Printf("--> %v %v\n", r.Method, r.URL)
// Construct filtered header to send to origin server
hh := http.Header{}
for _, hk := range passthruRequestHeaderKeys {
if hv, ok := r.Header[hk]; ok {
hh[hk] = hv
}
}
// Construct request to send to origin server
rr := http.Request{
Method: r.Method,
URL: r.URL,
Header: hh,
Body: r.Body,
// TODO: Is this correct for a 0 value?
// Perhaps a 0 may need to be reinterpreted as -1?
ContentLength: r.ContentLength,
Close: r.Close,
}
// Forward request to origin server
resp, err := http.DefaultTransport.RoundTrip(&rr)
if err != nil {
// TODO: Passthru more error information
http.Error(w, "Could not reach origin server", 500)
return
}
defer resp.Body.Close()
if (verbose) {
fmt.Printf("<-- %v %+v\n", resp.Status, resp.Header)
} else {
fmt.Printf("<-- %v\n", resp.Status)
}
// Transfer filtered header from origin server -> client
respH := w.Header()
for _, hk := range passthruResponseHeaderKeys {
if hv, ok := resp.Header[hk]; ok {
respH[hk] = hv
}
}
w.WriteHeader(resp.StatusCode)
// Transfer response from origin server -> client
if resp.ContentLength > 0 {
// (Ignore I/O errors, since there's nothing we can do)
io.CopyN(w, resp.Body, resp.ContentLength)
} else if (resp.Close) { // TODO: Is this condition right?
// Copy until EOF or some other error occurs
for {
if _, err := io.Copy(w, resp.Body); err != nil {
break
}
}
}
}
from goproxy.
from goproxy.
You are beginner of golang, and also http proxies; Through you say above, i have some suggestions for you with below:
1.a powerful http server library in golang, https://github.com/valyala/fasthttp
2.a powerful router of fasthttp https://github.com/buaazp/fasthttprouter
3.json encode/decode example in golang, https://golang.org/src/encoding/json/example_test.go
4.http request library,https://github.com/parnurzeal/gorequest
from goproxy.
Related Issues (20)
- Unexpected http response: '' when used as http proxy of git
- 两层TCP代理,想要拦截本地服务通过TCP访问的流量并代理转发,能够实现吗? HOT 1
- When I install it I get an error:./install.sh: line 25: 6067 Segmentation fault (core dumped) proxy keygen -C proxy > /dev/null 2>&1 HOT 2
- Access Admin Panel HOT 2
- 进程异常退出
- 商业版goproxy 能不能解决"Too Many Requests" 这个问题
- 无法解析IPV6地址,访问异常 HOT 2
- Feature request: add support TCP Brutal
- How to stop the daemon/forever HOT 3
- can this act as a forward proxy like squid ?
- 1
- Free version with -a and -f , who want's to participlate?
- 一条命令同时打开http和socks代理服务
- Limit the number of goroutines
- goproxy安装好后后台地址多少
- bad certificate HOT 2
- 能实现访问A域名走A代理,访问其它域名走B代理吗? HOT 1
- FR: How to pring to logs without colorized ANSI escape sequences HOT 2
- 怎么限制黑白域名名单 -b -d试了都没生效
- rhttp不支持https通过代理转发吗,麻烦支持下吧
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from goproxy.