Skip to content

Instantly share code, notes, and snippets.

@kikyous
Last active December 26, 2015 23:19
Show Gist options
  • Save kikyous/7229567 to your computer and use it in GitHub Desktop.
Save kikyous/7229567 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/elazarl/goproxy"
"log"
"net/http"
"regexp"
)
var players = map[string] string {
"youku_loader": "http://chat.saas.bdfzer.com/uploads/20131101171855-loader.swf",
"youku_player": "https://haoutil.googlecode.com/svn/trunk/player/testmod/player.swf",
"ku6": "https://haoutil.googlecode.com/svn/trunk/player/ku6.swf",
"ku6_out": "https://haoutil.googlecode.com/svn/trunk/player/ku6_out.swf",
"iqiyi": "https://haoutil.googlecode.com/svn/trunk/player/testmod/iqiyi.swf",
"iqiyi5": "https://haoutil.googlecode.com/svn/trunk/player/testmod/iqiyi5.swf",
"iqiyi_out": "https://haoutil.googlecode.com/svn/trunk/player/testmod/iqiyi_out.swf",
"tudou": "https://haoutil.googlecode.com/svn/trunk/player/testmod/tudou.swf",
"tudou_olc": "https://haoutil.googlecode.com/svn/trunk/player/testmod/olc_8.swf",
"tudou_sp": "https://haoutil.googlecode.com/svn/trunk/player/testmod/sp.swf",
"letv": "https://haoutil.googlecode.com/svn/trunk/player/testmod/letv.swf",
}
type Rule struct {
Match *regexp.Regexp
Replace string
}
func NewRule(match, replace string) *Rule {
return &Rule{regexp.MustCompile(match), replace, }
}
func (r *Rule)FindRule(url string) (matched bool, target string) {
matched = r.Match.MatchString(url)
target = r.Match.ReplaceAllString(url,r.Replace)
return
}
func main() {
// log.Println(regexp.MustCompile(`^\d(\w+) (\w+)?`).ReplaceAllString("3yasdfasdf ","-$1 + $2"))
rules := []Rule{
*NewRule(
`^static\.youku\.com(\/v[\d\.]+)?\/v\/swf\/loader\.swf`,
players["youku_loader"],
),
*NewRule(
`^static\.youku\.com(\/v[\d\.]+)?\/v\/swf\/q?player[^\.]*\.swf(\?.*)?`,
players["youku_player"] + "$2",
),
*NewRule(
`^player\.youku\.com\/player\.php\/.*sid\/([\w=]+).*(\/v)?\.swf.*`,
players["youku_player"] + "?showAd=0&VideoIDS=$1",
),
}
proxy := goproxy.NewProxyHttpServer()
proxy.OnRequest().DoFunc(
func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
url := r.URL.Host+r.URL.Path
for _, v := range rules {
matched, target := v.FindRule(url)
if matched {
log.Println(matched, target)
resp, _ := http.Get(target)
return r, resp
}
}
return r, nil
})
log.Fatalln(http.ListenAndServe(":8080", proxy))
}
@kikyous
Copy link
Author

kikyous commented Oct 30, 2013

这是一个go的代理服务器,正在研究。reddit不知道什么东西

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment