程序分为站点端和中心端(相当于一个proxy).
这个小功能主要是解决,程序经常让我们去拖日志,特别烦.所以做个小程序.使程序可以自己去线上查看.
有个问题是,不能把游戏服务器暴露出来,还有就是不能占用业务机器的公网带宽.
这个小程序主要就是中心端(proxy)通过内网获取文件,然后转发给访问端.
中心端主要有两个文件,一个是程序文件.一个是我命名为json的文件(主要是根据ID来分辨分站)
主程序文件内容:
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/rpc"
"regexp"
"strings"
"text/template"
"time"
)
var port *string = flag.String("l", "127.0.0.1:2789", "-l 127.0.0.1:2789")
var Index string
var Filelist string
type Info struct {
Name string
Time time.Time
}
type Newlist []*Info
type pathlist struct {
List Newlist
Dir string
}
var configlist map[string]string
var re *regexp.Regexp
func main() {
flag.Parse()
Index = fmt.Sprintf(`<html><title>分区列表</title><body><table>
{{range $k,$v := .}}<tr><td><a href="http://%s/{{$k}}/">分区:{{$k}}</a></td></tr>
{{end}}</table></body></html>`, *port)
Filelist = fmt.Sprintf(`<html>
<title>文件列表</title>
<body><table>
{{$path := .Dir}}{{range $k,$v := .List}}<tr><td><a href="http://%s{{$path}}/{{$v.Name}}">文件名:{{$v.Name}}</a></td><td> 修改时间:{{$v.Time}}</td></tr>
{{end}}</table></body></html>`, *port)
re, _ = regexp.Compile("^/[0-9]{7}/")
b, _ := ioutil.ReadFile("json")
json.Unmarshal(b, &configlist)
http.HandleFunc("/", route)
e := http.ListenAndServe(*port, nil)
fmt.Println(e)
}
func route(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
t := template.New("")
t.Parse(Index)
t.Execute(w, configlist)
}
if re.MatchString(r.URL.Path) {
getlist(r.URL.Path, w)
}
}
func getlist(path string, w http.ResponseWriter) {
l_path := strings.Split(path, "/")
id := l_path[1]
path = strings.Join(l_path[2:], "/")
client, err := rpc.DialHTTP("tcp", configlist[id])
if err != nil {
return
}
var x Newlist
err = client.Call("Info_list.List", path, &x)
if err != nil {
if err.Error() == path {
getfile(path, id, w)
return
}
fmt.Fprintln(w, err)
return
}
if path == "" {
path = "/" + id
} else {
path = "/" + id + "/" + path
}
var Pl pathlist = pathlist{x, path}
T := template.New("")
T.Parse(Filelist)
T.Execute(w, Pl)
}
func getfile(path, id string, w http.ResponseWriter) error {
r, e := http.Get(fmt.Sprintf("http://%s/%s", configlist[id], path))
if e != nil {
fmt.Println(e)
return e
}
io.Copy(w, r.Body)
return nil
}
</pre><p></p><pre>
json 文件内容:
{"7400006":"192.168.80.247:6987",
"7400007":"127.0.0.1:1987"}
分站程序:
目录结构我使用的是: --agent -|
---http.go
---sort.go
--maste.go
http.go内容:
package agent
import (
"errors"
"fmt"
"net/http"
"net/rpc"
"os"
)
var Http_path string
type Info_list int
func HttpServer(port, path string) {
Http_path = path
rpc.Register(new(Info_list))
rpc.HandleHTTP()
http.Handle("/", http.FileServer(http.Dir(path)))
err := http.ListenAndServe(fmt.Sprintf(port), nil)
fmt.Println(err)
}
func (i *Info_list) List(path string, result *Newlist) error {
path_l := Http_path + "/" + path
info, e := os.Stat(path_l)
if e != nil {
return e
}
if info.IsDir() {
L, _ := GetFilelist(path_l)
*result = L
return nil
} else {
return errors.New(path)
}
}
sort.go内容:
package agent
import (
"io/ioutil"
"sort"
"time"
)
type Info struct {
Name string
Time time.Time
}
type Newlist []*Info
func GetFilelist(path string) (Newlist, error) {
l, err := ioutil.ReadDir(path)
if err != nil {
return []*Info{}, err
}
var list []*Info
for _, v := range l {
list = append(list, &Info{v.Name(), v.ModTime()})
}
sort.Sort(Newlist(list))
return list, nil
}
func (I Newlist) Len() int {
return len(I)
}
func (I Newlist) Less(i, j int) bool {
return I[i].Time.Unix() < I[j].Time.Unix()
}
func (I Newlist) Swap(i, j int) {
I[i], I[j] = I[j], I[i]
}
maste.go内容:
package main
import (
"agent"
"flag"
)
func main() {
var port *string = flag.String("l", ":1789", "-l :1789 或者 -l 127.0.0.1:1789 默认是监听在1789的端口上")
var path *string = flag.String("p", "./", "-p ./ 或者 -p D:\\code")
flag.Parse()
go agent.HttpServer(*port, *path)
select {}
}
var configlist map[string]string
var re *regexp.Regexp