Initial commit

This commit is contained in:
zhiqiang feng
2020-05-11 14:58:22 +08:00
commit cf86eadf63
6 changed files with 434 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"gzzn.com/mini/ciimsproxy/internal"
)
const (
servicePrefix string = "/services/ExchangeService"
// defaultURL string = "http://localhost:8080"
defaultURL string = "http://192.168.10.96:8080"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.POST("/msg", sendMessage)
r.POST("/receive", receiveMessage)
// r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
r.Run(":9090")
}
func sendMessage(c *gin.Context) {
var message struct {
URL string `json:"url" `
User string `json:"user" binding:"required"`
Pass string `json:"pass" binding:"required"`
Event string `json:"event" binding:"required"`
Priority int `json:"priority" binding:"required"`
Val bool `json:"val"`
Msg string `json:"msg" binding:"required"`
}
err := c.Bind(&message)
if err == nil {
msg := internal.CreateSend(message.User, message.Pass,
message.Priority, message.Event, message.Val, message.Msg)
url := getURL(message.URL)
resp := internal.Send(url, msg)
c.String(http.StatusOK, resp)
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}
func receiveMessage(c *gin.Context) {
var message struct {
URL string `json:"url" `
User string `json:"user" binding:"required"`
Pass string `json:"pass" binding:"required"`
Count int `json:"count" binding:"required"`
}
err := c.Bind(&message)
if err == nil {
msg := internal.CreateReceive(message.User, message.Pass, message.Count)
url := getURL(message.URL)
resp := internal.Send(url, msg)
c.JSON(http.StatusOK, gin.H{"msgs": internal.GetMsgs(resp)})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}
func getURL(url string) string {
var result string
if len(url) > 0 {
result = url + servicePrefix
} else {
result = defaultURL + servicePrefix
}
return result
}
+8
View File
@@ -0,0 +1,8 @@
module gzzn.com/mini/ciimsproxy
go 1.13
require (
github.com/gin-gonic/gin v1.6.3
github.com/stretchr/testify v1.5.1
)
+48
View File
@@ -0,0 +1,48 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+132
View File
@@ -0,0 +1,132 @@
package internal
import (
"bytes"
"encoding/xml"
"regexp"
"strconv"
"strings"
)
const (
sendtpl = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<BHIA_CIIMS:AuthenticationToken xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">
<BHIA_CIIMS:Username xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##user##</BHIA_CIIMS:Username>
<BHIA_CIIMS:Password xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##pass##</BHIA_CIIMS:Password>
</BHIA_CIIMS:AuthenticationToken>
</soap:Header>
<soap:Body>
<ns1:send xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:in0>##msg##</ns1:in0>
<ns2:in1 xmlns="http://msg.ciims.bhia.itdcl.com" xmlns:ns2="http://ciims.bhia.itdcl.com/ExchangeService">
<ack>false</ack>
<priority>##priority##</priority>
<routeId>##event##</routeId>
<valXml>##xml##</valXml>
</ns2:in1>
</ns1:send>
</soap:Body>
</soap:Envelope>
`
receivetpl = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<BHIA_CIIMS:AuthenticationToken xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">
<BHIA_CIIMS:Username xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##user##</BHIA_CIIMS:Username>
<BHIA_CIIMS:Password xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##pass##</BHIA_CIIMS:Password>
</BHIA_CIIMS:AuthenticationToken>
</soap:Header>
<soap:Body>
<ns1:receive xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:in0>##count##</ns1:in0>
</ns1:receive>
</soap:Body>
</soap:Envelope>
`
msgExp = `<ns1:string>.*<\/ns1:string>`
)
// CreateSend xml string for sending
func CreateSend(user string, pass string, priority int, event string, valXML bool, message string) string {
var mb bytes.Buffer
xml.Escape(&mb, []byte(message))
msg := strings.Replace(sendtpl, "##user##", user, -1)
msg = strings.Replace(msg, "##pass##", pass, -1)
msg = strings.Replace(msg, "##event##", event, -1)
msg = strings.Replace(msg, "##priority##", strconv.Itoa(priority), -1)
msg = strings.Replace(msg, "##xml##", strconv.FormatBool(valXML), -1)
msg = strings.Replace(msg, "##msg##", mb.String(), -1)
return msg
}
//CreateReceive message
func CreateReceive(user string, pass string, count int) string {
msg := strings.Replace(receivetpl, "##user##", user, -1)
msg = strings.Replace(msg, "##pass##", pass, -1)
msg = strings.Replace(msg, "##count##", strconv.Itoa(count), -1)
return msg
}
func Map(vs []string, f func(string) string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
//GetMsgs get message from soap response
func GetMsgs(soap string) []string {
r, _ := regexp.Compile(msgExp)
msgs := r.FindAllString(soap, -1)
return Map(msgs, split)
}
func split(msg string) string {
if len(msg) > 12 {
return msg[12 : len(msg)-13]
} else {
return msg
}
}
+120
View File
@@ -0,0 +1,120 @@
package internal
import (
"testing"
"github.com/stretchr/testify/assert"
)
const (
SendEsp = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<BHIA_CIIMS:AuthenticationToken xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">
<BHIA_CIIMS:Username xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">FIMS</BHIA_CIIMS:Username>
<BHIA_CIIMS:Password xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">FIMSPASS</BHIA_CIIMS:Password>
</BHIA_CIIMS:AuthenticationToken>
</soap:Header>
<soap:Body>
<ns1:send xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:in0>&lt;MSG/&gt;</ns1:in0>
<ns2:in1 xmlns="http://msg.ciims.bhia.itdcl.com" xmlns:ns2="http://ciims.bhia.itdcl.com/ExchangeService">
<ack>false</ack>
<priority>1</priority>
<routeId>FLOP-CHDT-ORMS-FIMS</routeId>
<valXml>false</valXml>
</ns2:in1>
</ns1:send>
</soap:Body>
</soap:Envelope>
`
ReceiveEsp = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<BHIA_CIIMS:AuthenticationToken xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">
<BHIA_CIIMS:Username xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">FIMS</BHIA_CIIMS:Username>
<BHIA_CIIMS:Password xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">FIMSPASS</BHIA_CIIMS:Password>
</BHIA_CIIMS:AuthenticationToken>
</soap:Header>
<soap:Body>
<ns1:receive xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:in0>2</ns1:in0>
</ns1:receive>
</soap:Body>
</soap:Envelope>
`
ReceiveResp string = `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:receiveResponse xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:out>
<ns1:string>&lt;?xml version="1.0" encoding="UTF-8"?> &lt;msg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="unisysaodbsis.xsd"> &lt;meta> &lt;sndr>AODB&lt;/sndr> &lt;seqn>820830&lt;/seqn> &lt;dttm>20180527124442&lt;/dttm> &lt;type>FLOP&lt;/type> &lt;styp>ESTT&lt;/styp> &lt;/meta> &lt;flop> &lt;flid>10725849&lt;/flid> &lt;ffid>CA-CA4242-A-27MAY181955-D&lt;/ffid> &lt;estt>27MAY181945&lt;/estt> &lt;/flop> &lt;/msg></ns1:string>
<ns1:string>&lt;?xml version="1.0" encoding="UTF-8"?> &lt;msg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="unisysaodbsis.xsd"> &lt;meta> &lt;sndr>AODB&lt;/sndr> &lt;seqn>820830&lt;/seqn> &lt;dttm>20180527124442&lt;/dttm> &lt;type>FLOP&lt;/type> &lt;styp>ESTT&lt;/styp> &lt;/meta> &lt;flop> &lt;flid>10725849&lt;/flid> &lt;ffid>CA-CA4242-A-27MAY181955-D&lt;/ffid> &lt;estt>27MAY181945&lt;/estt> &lt;/flop> &lt;/msg></ns1:string>
</ns1:out>
</ns1:receiveResponse>
</soap:Body>
</soap:Envelope>`
Msg string = `<?xml version="1.0" encoding="UTF-8"?> <msg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="unisysaodbsis.xsd"> <meta> <sndr>AODB</sndr> <seqn>820830</seqn> <dttm>20180527124442</dttm> <type>FLOP</type> <styp>ESTT</styp> </meta> <flop> <flid>10725849</flid> <ffid>CA-CA4242-A-27MAY181955-D</ffid> <estt>27MAY181945</estt> </flop> </msg>`
)
func TestSend(t *testing.T) {
assert := assert.New(t)
// assert equality
// assert.Equal(123, 123, "they should be equal")
msg := CreateSend("FIMS", "FIMSPASS", 1, "FLOP-CHDT-ORMS-FIMS", false, "<MSG/>")
// println(msg)
assert.Equal(SendEsp, msg, "send post should be equal")
}
func TestReceive(t *testing.T) {
assert := assert.New(t)
msg := CreateReceive("FIMS", "FIMSPASS", 2)
assert.Equal(ReceiveEsp, msg, "receive post should be equal")
}
func TestGetMsg(t *testing.T) {
assert := assert.New(t)
msgs := GetMsgs(ReceiveResp)
assert.Equal(2, len(msgs), "should get 2 message")
assert.Equal(Msg, msgs[0], "message")
}
+49
View File
@@ -0,0 +1,49 @@
package internal
import (
"bytes"
"io/ioutil"
"net/http"
"strings"
"time"
)
const (
defaultContentType string = "text/xml; charset=UTF-8"
defaultAgent string = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; XFire Client +http://xfire.codehaus.org)"
)
//Post xml string to url
func post(url string, contentType string, msg string) (*http.Response, error) {
return http.Post(url, contentType, bytes.NewBuffer([]byte(msg)))
}
func postSim(url string, msg string) string {
timeout := time.Duration(10 * time.Second)
client := http.Client{
Timeout: timeout,
}
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(msg))
if err != nil {
return err.Error()
}
req.Header.Set("Content-Type", defaultContentType)
req.Header.Set("User-Agent", defaultAgent)
req.Header.Set("SOAPAction", "")
resp, err := client.Do(req)
if err != nil {
return err.Error()
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err.Error()
}
return string(respBytes)
}
//Send message
func Send(url string, message string) string {
return postSim(url, message)
}