From 8797f2a33022873a0744a0f84675df1e806e70bb Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Thu, 14 Aug 2014 14:15:52 +0800 Subject: [PATCH] (+)add project files --- conf/app.conf | 3 + controllers/default.go | 249 +++++++++++++++++++++++++++++++++++++++++ main.go | 20 ++++ routers/router.go | 25 +++++ tests/default_test.go | 37 ++++++ views/all.tpl | 12 ++ views/alljson.tpl | 14 +++ views/allxml.tpl | 16 +++ views/index.tpl | 161 ++++++++++++++++++++++++++ views/iponly.tpl | 1 + views/value.tpl | 1 + 11 files changed, 539 insertions(+) create mode 100644 conf/app.conf create mode 100644 controllers/default.go create mode 100644 main.go create mode 100644 routers/router.go create mode 100644 tests/default_test.go create mode 100644 views/all.tpl create mode 100644 views/alljson.tpl create mode 100644 views/allxml.tpl create mode 100644 views/index.tpl create mode 100644 views/iponly.tpl create mode 100644 views/value.tpl diff --git a/conf/app.conf b/conf/app.conf new file mode 100644 index 0000000..a0a46c0 --- /dev/null +++ b/conf/app.conf @@ -0,0 +1,3 @@ +appname = ifconfig +httpport = 8080 +runmode = pro diff --git a/controllers/default.go b/controllers/default.go new file mode 100644 index 0000000..caa2925 --- /dev/null +++ b/controllers/default.go @@ -0,0 +1,249 @@ +package controllers + +import ( + "bytes" + "github.com/astaxie/beego" + "strings" +) + +type MainController struct { + beego.Controller +} + +func (this *MainController) GetForwarded() { + if len(this.Ctx.Request.Header["X-Forwarded-For"]) > 0 { + this.Data["Value"] = this.Ctx.Request.Header["X-Forwarded-For"][0] + } + this.TplNames = "value.tpl" +} + +func (this *MainController) GetHost() { + this.Data["Value"] = this.Ctx.Request.Host + this.TplNames = "value.tpl" +} + +func (this *MainController) GetIP() { + this.Data["Value"] = this.Ctx.Input.IP() + this.TplNames = "value.tpl" +} + +func (this *MainController) GetPort() { + remote_addr := []byte(this.Ctx.Request.RemoteAddr) + pos := bytes.IndexByte(remote_addr, ':') + this.Data["Value"] = string(remote_addr[pos+1:]) + this.TplNames = "value.tpl" +} + +func (this *MainController) GetVia() { + if len(this.Ctx.Request.Header["Via"]) > 0 { + this.Data["Value"] = this.Ctx.Request.Header["Via"][0] + } + this.TplNames = "value.tpl" +} + +func (this *MainController) GetMime() { + if len(this.Ctx.Request.Header["Accept"]) > 0 { + this.Data["Value"] = this.Ctx.Request.Header["Accept"][0] + } + this.TplNames = "value.tpl" +} + +func (this *MainController) GetLang() { + if len(this.Ctx.Request.Header["Accept-Language"]) > 0 { + this.Data["Value"] = this.Ctx.Request.Header["Accept-Language"][0] + } + this.TplNames = "value.tpl" +} + +func (this *MainController) GetCharset() { + if len(this.Ctx.Request.Header["Charset"]) > 0 { + this.Data["Value"] = this.Ctx.Request.Header["Charset"][0] + } + this.TplNames = "value.tpl" +} + +func (this *MainController) GetEncoding() { + if len(this.Ctx.Request.Header["Accept-Encoding"]) > 0 { + this.Data["Value"] = this.Ctx.Request.Header["Accept-Encoding"][0] + } + this.TplNames = "value.tpl" +} + +func (this *MainController) GetUserAgent() { + this.Data["Value"] = this.Ctx.Request.UserAgent() + this.TplNames = "value.tpl" +} + +func (this *MainController) GetConnection() { + if len(this.Ctx.Request.Header["Connection"]) > 0 { + this.Data["Value"] = this.Ctx.Request.Header["Connection"][0] + } + this.TplNames = "value.tpl" +} + +func (this *MainController) GetKeepAlive() { + if len(this.Ctx.Request.Header["KeepAlive"]) > 0 { + this.Data["Value"] = this.Ctx.Request.Header["KeepAlive"][0] + } + this.TplNames = "value.tpl" +} + +func (this *MainController) GetAll() { + this.Data["Email"] = "missdeer@dfordsoft.com" + this.Data["UserAgent"] = this.Ctx.Request.UserAgent() + this.Data["Host"] = this.Ctx.Request.Host + this.Data["IP"] = this.Ctx.Input.IP() + remote_addr := []byte(this.Ctx.Request.RemoteAddr) + pos := bytes.IndexByte(remote_addr, ':') + this.Data["Port"] = string(remote_addr[pos+1:]) + this.Data["Method"] = this.Ctx.Request.Method + if len(this.Ctx.Request.Header["Accept-Encoding"]) > 0 { + this.Data["Encoding"] = this.Ctx.Request.Header["Accept-Encoding"][0] + } + if len(this.Ctx.Request.Header["Accept"]) > 0 { + this.Data["Mime"] = this.Ctx.Request.Header["Accept"][0] + } + if len(this.Ctx.Request.Header["Connection"]) > 0 { + this.Data["Connection"] = this.Ctx.Request.Header["Connection"][0] + } + if len(this.Ctx.Request.Header["Via"]) > 0 { + this.Data["Via"] = this.Ctx.Request.Header["Via"][0] + } + if len(this.Ctx.Request.Header["Charset"]) > 0 { + this.Data["Charset"] = this.Ctx.Request.Header["Charset"][0] + } + if len(this.Ctx.Request.Header["KeepAlive"]) > 0 { + this.Data["Keepalive"] = this.Ctx.Request.Header["KeepAlive"][0] + } + if len(this.Ctx.Request.Header["X-Forwarded-For"]) > 0 { + this.Data["Forwarded"] = this.Ctx.Request.Header["X-Forwarded-For"][0] + } + if len(this.Ctx.Request.Header["Accept-Language"]) > 0 { + this.Data["Lang"] = this.Ctx.Request.Header["Accept-Language"][0] + } + this.Data["Referer"] = this.Ctx.Input.Refer() + + this.TplNames = "all.tpl" +} + +func (this *MainController) GetAllXML() { + thisData := make(map[string]interface{}) + thisData["Email"] = "missdeer@dfordsoft.com" + thisData["UserAgent"] = this.Ctx.Request.UserAgent() + thisData["Host"] = this.Ctx.Request.Host + thisData["IP"] = this.Ctx.Input.IP() + remote_addr := []byte(this.Ctx.Request.RemoteAddr) + pos := bytes.IndexByte(remote_addr, ':') + thisData["Port"] = string(remote_addr[pos+1:]) + thisData["Method"] = this.Ctx.Request.Method + if len(this.Ctx.Request.Header["Accept-Encoding"]) > 0 { + thisData["Encoding"] = this.Ctx.Request.Header["Accept-Encoding"][0] + } + if len(this.Ctx.Request.Header["Accept"]) > 0 { + thisData["Mime"] = this.Ctx.Request.Header["Accept"][0] + } + if len(this.Ctx.Request.Header["Connection"]) > 0 { + thisData["Connection"] = this.Ctx.Request.Header["Connection"][0] + } + if len(this.Ctx.Request.Header["Via"]) > 0 { + thisData["Via"] = this.Ctx.Request.Header["Via"][0] + } + if len(this.Ctx.Request.Header["Charset"]) > 0 { + thisData["Charset"] = this.Ctx.Request.Header["Charset"][0] + } + if len(this.Ctx.Request.Header["KeepAlive"]) > 0 { + thisData["Keepalive"] = this.Ctx.Request.Header["KeepAlive"][0] + } + if len(this.Ctx.Request.Header["X-Forwarded-For"]) > 0 { + thisData["Forwarded"] = this.Ctx.Request.Header["X-Forwarded-For"][0] + } + if len(this.Ctx.Request.Header["Accept-Language"]) > 0 { + thisData["Lang"] = this.Ctx.Request.Header["Accept-Language"][0] + } + thisData["Referer"] = this.Ctx.Input.Refer() + + this.Data["xml"] = thisData + this.ServeXml() +} + +func (this *MainController) GetAllJSON() { + thisData := make(map[string]interface{}) + thisData["Email"] = "missdeer@dfordsoft.com" + thisData["UserAgent"] = this.Ctx.Request.UserAgent() + thisData["Host"] = this.Ctx.Request.Host + thisData["IP"] = this.Ctx.Input.IP() + remote_addr := []byte(this.Ctx.Request.RemoteAddr) + pos := bytes.IndexByte(remote_addr, ':') + thisData["Port"] = string(remote_addr[pos+1:]) + thisData["Method"] = this.Ctx.Request.Method + if len(this.Ctx.Request.Header["Accept-Encoding"]) > 0 { + thisData["Encoding"] = this.Ctx.Request.Header["Accept-Encoding"][0] + } + if len(this.Ctx.Request.Header["Accept"]) > 0 { + thisData["Mime"] = this.Ctx.Request.Header["Accept"][0] + } + if len(this.Ctx.Request.Header["Connection"]) > 0 { + thisData["Connection"] = this.Ctx.Request.Header["Connection"][0] + } + if len(this.Ctx.Request.Header["Via"]) > 0 { + thisData["Via"] = this.Ctx.Request.Header["Via"][0] + } + if len(this.Ctx.Request.Header["Charset"]) > 0 { + thisData["Charset"] = this.Ctx.Request.Header["Charset"][0] + } + if len(this.Ctx.Request.Header["KeepAlive"]) > 0 { + thisData["Keepalive"] = this.Ctx.Request.Header["KeepAlive"][0] + } + if len(this.Ctx.Request.Header["X-Forwarded-For"]) > 0 { + thisData["Forwarded"] = this.Ctx.Request.Header["X-Forwarded-For"][0] + } + if len(this.Ctx.Request.Header["Accept-Language"]) > 0 { + thisData["Lang"] = this.Ctx.Request.Header["Accept-Language"][0] + } + thisData["Referer"] = this.Ctx.Input.Refer() + + this.Data["json"] = thisData + this.ServeJson() +} + +func (this *MainController) Get() { + this.Data["Email"] = "missdeer@dfordsoft.com" + this.Data["UserAgent"] = this.Ctx.Request.UserAgent() + this.Data["Host"] = this.Ctx.Request.Host + this.Data["IP"] = this.Ctx.Input.IP() + remote_addr := []byte(this.Ctx.Request.RemoteAddr) + pos := bytes.IndexByte(remote_addr, ':') + this.Data["Port"] = string(remote_addr[pos+1:]) + this.Data["Method"] = this.Ctx.Request.Method + if len(this.Ctx.Request.Header["Accept-Encoding"]) > 0 { + this.Data["Encoding"] = this.Ctx.Request.Header["Accept-Encoding"][0] + } + if len(this.Ctx.Request.Header["Accept"]) > 0 { + this.Data["Mime"] = this.Ctx.Request.Header["Accept"][0] + } + if len(this.Ctx.Request.Header["Connection"]) > 0 { + this.Data["Connection"] = this.Ctx.Request.Header["Connection"][0] + } + if len(this.Ctx.Request.Header["Via"]) > 0 { + this.Data["Via"] = this.Ctx.Request.Header["Via"][0] + } + if len(this.Ctx.Request.Header["Charset"]) > 0 { + this.Data["Charset"] = this.Ctx.Request.Header["Charset"][0] + } + if len(this.Ctx.Request.Header["KeepAlive"]) > 0 { + this.Data["Keepalive"] = this.Ctx.Request.Header["KeepAlive"][0] + } + if len(this.Ctx.Request.Header["X-Forwarded-For"]) > 0 { + this.Data["Forwarded"] = this.Ctx.Request.Header["X-Forwarded-For"][0] + } + if len(this.Ctx.Request.Header["Accept-Language"]) > 0 { + this.Data["Lang"] = this.Ctx.Request.Header["Accept-Language"][0] + } + this.Data["Referer"] = this.Ctx.Input.Refer() + + if strings.Contains(this.Ctx.Request.UserAgent(), "curl") { + this.TplNames = "iponly.tpl" + } else { + this.TplNames = "index.tpl" + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..c114f14 --- /dev/null +++ b/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "github.com/astaxie/beego" + _ "github.com/missdeer/ifconfig/routers" + "os" +) + +const ( + HostVar = "VCAP_APP_HOST" + PortVar = "VCAP_APP_PORT" +) + +func main() { + var port string + if port = os.Getenv(PortVar); port == "" { + port = "8080" + } + beego.Run(":" + port) +} diff --git a/routers/router.go b/routers/router.go new file mode 100644 index 0000000..17646ae --- /dev/null +++ b/routers/router.go @@ -0,0 +1,25 @@ +package routers + +import ( + "github.com/astaxie/beego" + "github.com/missdeer/ifconfig/controllers" +) + +func init() { + beego.Router("/", &controllers.MainController{}, "get:Get") + beego.Router("/ip", &controllers.MainController{}, "get:GetIP") + beego.Router("/host", &controllers.MainController{}, "get:GetHost") + beego.Router("/ua", &controllers.MainController{}, "get:GetUserAgent") + beego.Router("/port", &controllers.MainController{}, "get:GetPort") + beego.Router("/keepalive", &controllers.MainController{}, "get:GetKeepAlive") + beego.Router("/lang", &controllers.MainController{}, "get:GetLang") + beego.Router("/connection", &controllers.MainController{}, "get:GetConnection") + beego.Router("/encoding", &controllers.MainController{}, "get:GetEncoding") + beego.Router("/via", &controllers.MainController{}, "get:GetVia") + beego.Router("/mime", &controllers.MainController{}, "get:GetMime") + beego.Router("/charset", &controllers.MainController{}, "get:GetCharset") + beego.Router("/forwarded", &controllers.MainController{}, "get:GetForwarded") + beego.Router("/all", &controllers.MainController{}, "get:GetAll") + beego.Router("/all.xml", &controllers.MainController{}, "get:GetAllXML") + beego.Router("/all.json", &controllers.MainController{}, "get:GetAllJSON") +} diff --git a/tests/default_test.go b/tests/default_test.go new file mode 100644 index 0000000..31b9de6 --- /dev/null +++ b/tests/default_test.go @@ -0,0 +1,37 @@ +package test + +import ( + _ "github.com/missdeer/ifconfig/routers" + "net/http" + "net/http/httptest" + "path/filepath" + "runtime" + "testing" + + "github.com/astaxie/beego" + . "github.com/smartystreets/goconvey/convey" +) + +func init() { + _, file, _, _ := runtime.Caller(1) + apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator)))) + beego.TestBeegoInit(apppath) +} + +// TestMain is a sample to run an endpoint test +func TestMain(t *testing.T) { + r, _ := http.NewRequest("GET", "/", nil) + w := httptest.NewRecorder() + beego.BeeApp.Handlers.ServeHTTP(w, r) + + beego.Trace("testing", "TestMain", "Code[%d]\n%s", w.Code, w.Body.String()) + + Convey("Subject: Test Station Endpoint\n", t, func() { + Convey("Status Code Should Be 200", func() { + So(w.Code, ShouldEqual, 200) + }) + Convey("The Result Should Not Be Empty", func() { + So(w.Body.Len(), ShouldBeGreaterThan, 0) + }) + }) +} diff --git a/views/all.tpl b/views/all.tpl new file mode 100644 index 0000000..b210c71 --- /dev/null +++ b/views/all.tpl @@ -0,0 +1,12 @@ +ip_addr: {{.IP}} +remote_host: {{.Host}} +user_agent: {{.UserAgent}} +port: {{.Port}} +lang: {{.Lang}} +connection: {{.Connection}} +keep_alive: {{.Keepalive}} +encoding: {{.Encoding}} +mime: {{.Mime}} +charset: {{.Charset}} +via: {{.Via}} +forwarded: {{.Forwarded}} diff --git a/views/alljson.tpl b/views/alljson.tpl new file mode 100644 index 0000000..30ce203 --- /dev/null +++ b/views/alljson.tpl @@ -0,0 +1,14 @@ +{ + "connection": "{{.Connection}}", + "ip_addr": "{{.IP}}", + "lang": "{{.Lang}}", + "remote_host": "{{.Host}}", + "user_agent": "{{.UserAgent}}", + "charset": "{{.Charset}}", + "port": "{{.Port}}", + "via": "{{.Via}}", + "forwarded": "{{.Forwarded}}", + "mime": "{{.Mime}}", + "keep_alive": "{{.Keepalive}}", + "encoding": "{{.Encoding}}" +} diff --git a/views/allxml.tpl b/views/allxml.tpl new file mode 100644 index 0000000..7113414 --- /dev/null +++ b/views/allxml.tpl @@ -0,0 +1,16 @@ + +<info> +<charset>{{.Charset}}</charset> +<connection>{{.Connection}}</connection> +<encoding>{{.Encoding}}</encoding> +<forwarded>{{.Forwarded}}</forwarded> +<ip_addr>{{.IP}}</ip_addr> +<keep_alive>{{.Keepalive}}</keep_alive> +<lang>{{.Lang}}</lang> +<mime>{{.Mime}}</mime> +<port>{{.Port}}</port> +<remote_host>{{.Host}}</remote_host> +<user_agent>{{.UserAgent}}</user_agent> +<via>{{.Via}}</via> +</info> + diff --git a/views/index.tpl b/views/index.tpl new file mode 100644 index 0000000..2226378 --- /dev/null +++ b/views/index.tpl @@ -0,0 +1,161 @@ + + + + + + + + + + + + + + + What Is My IP Address? - Yet Another ifconfig + + + +
+ +
+ + +
+
+

Your Connection

+ + + + + + + + + + + + + + + + + + + +
IP Address{{.IP}} + + +
Remote Host{{.Host}}
User Agent{{.UserAgent}}
Port{{.Port}}
Language{{.Lang}}
Referer{{.Referer}}
Connection{{.Connection}}
KeepAlive{{.Keepalive}}
Method{{.Method}}
Encoding{{.Encoding}}
MIME Type{{.Mime}}
Charset{{.Charset}}
Via{{.Via}}
X-Forwarded-For{{.Forwarded}}
+
+
+ + +
+
+

Command Line Interface

+ + + + + + + + + + + + + + + + + +
$ curl ifconfig.jd-app.com{{.IP}}
$ curl ifconfig.jd-app.com/ip{{.IP}}
$ curl ifconfig.jd-app.com/host{{.Host}}
$ curl ifconfig.jd-app.com/ua{{.UserAgent}}
$ curl ifconfig.jd-app.com/port{{.Port}}
$ curl ifconfig.jd-app.com/lang{{.Lang}}
$ curl ifconfig.jd-app.com/keepalive{{.Keepalive}}
$ curl ifconfig.jd-app.com/connection{{.Connection}}
$ curl ifconfig.jd-app.com/encoding{{.Encoding}}
$ curl ifconfig.jd-app.com/mime{{.Mime}}
$ curl ifconfig.jd-app.com/charset{{.Charset}}
$ curl ifconfig.jd-app.com/via{{.Via}}
$ curl ifconfig.jd-app.com/forwarded{{.Forwarded}}
$ curl ifconfig.jd-app.com/allip_addr: {{.IP}}
remote_host: {{.Host}}
user_agent: {{.UserAgent}}
port: {{.Port}}
lang: {{.Lang}}
connection: {{.Connection}}
keep_alive: {{.Keepalive}}
encoding: {{.Encoding}}
mime: {{.Mime}}
charset: {{.Charset}}
via: {{.Via}}
forwarded: {{.Forwarded}}
$ curl ifconfig.jd-app.com/all.xml<info>
    <charset>{{.Charset}}</charset>
    <connection>{{.Connection}}</connection>
    <encoding>{{.Encoding}}</encoding>
    <forwarded>{{.Forwarded}}</forwarded>
    <ip_addr>{{.IP}}</ip_addr>
    <keep_alive>{{.Keepalive}}</keep_alive>
    <lang>{{.Lang}}</lang>
    <mime>{{.Mime}}</mime>
    <port>{{.Port}}</port>
    <remote_host>{{.Host}}</remote_host>
    <user_agent>{{.UserAgent}}</user_agent>
    <via>{{.Via}}</via>
+</info>
+
$ curl ifconfig.jd-app.com/all.json{"connection":"{{.Connection}}","ip_addr":"{{.IP}}","lang":"{{.Lang}}","remote_host":"{{.Host}}","user_agent":"{{.UserAgent}}","charset":"{{.Charset}}","port":"{{.Port}}","via":"{{.Via}}","forwarded":"{{.Forwarded}}","mime":"{{.Mime}}","keep_alive":"{{.Keepalive}}","encoding":"{{.Encoding}}"}
+
+ +
+ + + diff --git a/views/iponly.tpl b/views/iponly.tpl new file mode 100644 index 0000000..79c4b79 --- /dev/null +++ b/views/iponly.tpl @@ -0,0 +1 @@ +{{.IP}} diff --git a/views/value.tpl b/views/value.tpl new file mode 100644 index 0000000..6231ccf --- /dev/null +++ b/views/value.tpl @@ -0,0 +1 @@ +{{.Value}}