1
0
Fork 0
mirror of https://git.envs.net/envs/ifconfig synced 2024-05-09 05:26:02 +02:00

(+)add project files

This commit is contained in:
Fan Yang 2014-08-14 14:15:52 +08:00
parent 0f0349ee14
commit 8797f2a330
11 changed files with 539 additions and 0 deletions

3
conf/app.conf Normal file
View File

@ -0,0 +1,3 @@
appname = ifconfig
httpport = 8080
runmode = pro

249
controllers/default.go Normal file
View File

@ -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"
}
}

20
main.go Normal file
View File

@ -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)
}

25
routers/router.go Normal file
View File

@ -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")
}

37
tests/default_test.go Normal file
View File

@ -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)
})
})
}

12
views/all.tpl Normal file
View File

@ -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}}

14
views/alljson.tpl Normal file
View File

@ -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}}"
}

16
views/allxml.tpl Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
&lt;info&gt;
&lt;charset&gt;{{.Charset}}&lt;/charset&gt;
&lt;connection&gt;{{.Connection}}&lt;/connection&gt;
&lt;encoding&gt;{{.Encoding}}&lt;/encoding&gt;
&lt;forwarded&gt;{{.Forwarded}}&lt;/forwarded&gt;
&lt;ip_addr&gt;{{.IP}}&lt;/ip_addr&gt;
&lt;keep_alive&gt;{{.Keepalive}}&lt;/keep_alive&gt;
&lt;lang&gt;{{.Lang}}&lt;/lang&gt;
&lt;mime&gt;{{.Mime}}&lt;/mime&gt;
&lt;port&gt;{{.Port}}&lt;/port&gt;
&lt;remote_host&gt;{{.Host}}&lt;/remote_host&gt;
&lt;user_agent&gt;{{.UserAgent}}&lt;/user_agent&gt;
&lt;via&gt;{{.Via}}&lt;/via&gt;
&lt;/info&gt;

161
views/index.tpl Normal file

File diff suppressed because one or more lines are too long

1
views/iponly.tpl Normal file
View File

@ -0,0 +1 @@
{{.IP}}

1
views/value.tpl Normal file
View File

@ -0,0 +1 @@
{{.Value}}