2020-05-11 14:58:22 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-08 15:17:07 +08:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2020-05-11 14:58:22 +08:00
|
|
|
"net/http"
|
2020-09-15 10:24:08 +08:00
|
|
|
"os"
|
2026-07-08 15:17:07 +08:00
|
|
|
"strconv"
|
2020-05-11 14:58:22 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"gzzn.com/mini/ciimsproxy/internal"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2026-07-08 15:17:07 +08:00
|
|
|
servicePrefix string = "/services/ExchangeService"
|
|
|
|
|
defaultTimeout = 240
|
|
|
|
|
maxMsgLen = 1048576 // 1MB
|
|
|
|
|
maxCount = 1000
|
2020-05-11 14:58:22 +08:00
|
|
|
)
|
|
|
|
|
|
2026-07-08 15:17:07 +08:00
|
|
|
type Config struct {
|
|
|
|
|
ServerURL string
|
|
|
|
|
Listen string
|
|
|
|
|
Timeout int
|
|
|
|
|
Client *internal.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type App struct {
|
|
|
|
|
config Config
|
|
|
|
|
}
|
2020-09-15 10:24:08 +08:00
|
|
|
|
2020-05-11 14:58:22 +08:00
|
|
|
func main() {
|
2026-07-08 15:17:07 +08:00
|
|
|
config, err := loadConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("[ERROR] configuration error: %v", err)
|
|
|
|
|
os.Exit(1)
|
2020-09-15 10:24:08 +08:00
|
|
|
}
|
2026-07-08 15:17:07 +08:00
|
|
|
|
|
|
|
|
log.Printf("use ciims: %s", config.ServerURL)
|
|
|
|
|
r := newRouter(config)
|
|
|
|
|
log.Printf("Starting ciims proxy for %s", config.ServerURL)
|
|
|
|
|
log.Printf("timeout: %d", config.Timeout)
|
|
|
|
|
if err := r.Run(config.Listen); err != nil {
|
|
|
|
|
log.Printf("[ERROR] server failed: %v", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadConfig() (Config, error) {
|
|
|
|
|
config := Config{
|
|
|
|
|
ServerURL: os.Getenv("CIIMS_SERVER"),
|
|
|
|
|
Listen: os.Getenv("PROXY_LISTEN"),
|
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
|
}
|
|
|
|
|
t, err := getIntEnv("CIIMS_TIMEOUT")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Config{}, err
|
|
|
|
|
}
|
|
|
|
|
if t > 0 {
|
|
|
|
|
config.Timeout = t
|
|
|
|
|
}
|
|
|
|
|
if config.Listen == "" {
|
|
|
|
|
config.Listen = ":9090"
|
|
|
|
|
}
|
|
|
|
|
config.Client, err = internal.NewClient(config.Timeout)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Config{}, err
|
|
|
|
|
}
|
|
|
|
|
return config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newRouter(config Config) *gin.Engine {
|
|
|
|
|
app := &App{config: config}
|
2020-05-11 14:58:22 +08:00
|
|
|
r := gin.Default()
|
|
|
|
|
r.GET("/ping", func(c *gin.Context) {
|
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
|
"message": "pong",
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-07-08 15:17:07 +08:00
|
|
|
r.POST("/send", app.sendMessage)
|
|
|
|
|
r.POST("/receive", app.receiveMessage)
|
|
|
|
|
return r
|
2020-05-11 14:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-08 15:17:07 +08:00
|
|
|
func (a *App) sendMessage(c *gin.Context) {
|
|
|
|
|
// Limit request body size
|
|
|
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxMsgLen)
|
|
|
|
|
|
2020-05-11 14:58:22 +08:00
|
|
|
var message struct {
|
2026-07-08 15:17:07 +08:00
|
|
|
URL string `json:"url"`
|
2020-05-11 14:58:22 +08:00
|
|
|
User string `json:"user" binding:"required"`
|
|
|
|
|
Pass string `json:"pass" binding:"required"`
|
|
|
|
|
Event string `json:"event" binding:"required"`
|
2026-07-08 15:17:07 +08:00
|
|
|
Priority int `json:"priority"`
|
2020-05-11 14:58:22 +08:00
|
|
|
Val bool `json:"val"`
|
|
|
|
|
Msg string `json:"msg" binding:"required"`
|
|
|
|
|
}
|
2026-07-08 15:17:07 +08:00
|
|
|
if err := c.ShouldBind(&message); err != nil {
|
|
|
|
|
a.handleBindError(c, err)
|
|
|
|
|
return
|
2020-05-11 14:58:22 +08:00
|
|
|
}
|
2026-07-08 15:17:07 +08:00
|
|
|
msg := internal.CreateSend(message.User, message.Pass,
|
|
|
|
|
message.Priority, message.Event, message.Val, message.Msg)
|
|
|
|
|
url := a.getURL(message.URL)
|
|
|
|
|
resp, err := a.config.Client.Send(url, msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
a.handleSendError(c, "send", resp, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
errMsg := resp.ErrorMessage()
|
|
|
|
|
if len(errMsg) > 0 {
|
|
|
|
|
log.Printf("[ERROR] SOAP fault: %s", errMsg)
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": errMsg})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"error": ""})
|
2020-05-11 14:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-08 15:17:07 +08:00
|
|
|
func (a *App) receiveMessage(c *gin.Context) {
|
|
|
|
|
// Limit request body size
|
|
|
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxMsgLen)
|
|
|
|
|
|
2020-05-11 14:58:22 +08:00
|
|
|
var message struct {
|
2026-07-08 15:17:07 +08:00
|
|
|
URL string `json:"url"`
|
2020-05-11 14:58:22 +08:00
|
|
|
User string `json:"user" binding:"required"`
|
|
|
|
|
Pass string `json:"pass" binding:"required"`
|
|
|
|
|
Count int `json:"count" binding:"required"`
|
|
|
|
|
}
|
2026-07-08 15:17:07 +08:00
|
|
|
if err := c.ShouldBind(&message); err != nil {
|
|
|
|
|
a.handleBindError(c, err)
|
|
|
|
|
return
|
2020-05-11 14:58:22 +08:00
|
|
|
}
|
2026-07-08 15:17:07 +08:00
|
|
|
|
|
|
|
|
if message.Count < 1 || message.Count > maxCount {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "count must be between 1 and " + strconv.Itoa(maxCount)})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg := internal.CreateReceive(message.User, message.Pass, message.Count)
|
|
|
|
|
url := a.getURL(message.URL)
|
|
|
|
|
resp, err := a.config.Client.Send(url, msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
a.handleSendError(c, "receive", resp, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if errMsg := resp.ErrorMessage(); errMsg != "" {
|
|
|
|
|
log.Printf("[ERROR] SOAP fault: %s", errMsg)
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": errMsg})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"msgs": resp.Messages()})
|
|
|
|
|
|
2020-05-11 14:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-08 15:17:07 +08:00
|
|
|
func (a *App) getURL(url string) string {
|
2020-05-11 14:58:22 +08:00
|
|
|
var result string
|
|
|
|
|
if len(url) > 0 {
|
|
|
|
|
result = url + servicePrefix
|
|
|
|
|
} else {
|
2026-07-08 15:17:07 +08:00
|
|
|
result = a.config.ServerURL + servicePrefix
|
2020-05-11 14:58:22 +08:00
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
2026-07-08 15:17:07 +08:00
|
|
|
|
|
|
|
|
func (a *App) handleBindError(c *gin.Context, err error) {
|
|
|
|
|
var maxBytesErr *http.MaxBytesError
|
|
|
|
|
if errors.As(err, &maxBytesErr) {
|
|
|
|
|
c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.Print(err.Error())
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) handleSendError(c *gin.Context, operation string, resp *internal.CIIMSResponse, err error) {
|
|
|
|
|
if resp != nil {
|
|
|
|
|
if errMsg := resp.ErrorMessage(); errMsg != "" {
|
|
|
|
|
log.Printf("[ERROR] SOAP fault: %s", errMsg)
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": errMsg})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var statusErr *internal.HTTPStatusError
|
|
|
|
|
if errors.As(err, &statusErr) {
|
|
|
|
|
if errMsg := internal.GetErrMsg(statusErr.Body); errMsg != "" {
|
|
|
|
|
log.Printf("[ERROR] SOAP fault: %s", errMsg)
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": errMsg})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.Printf("[ERROR] %s failed: %v", operation, err)
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getIntEnv(key string) (int, error) {
|
|
|
|
|
val := os.Getenv(key)
|
|
|
|
|
if val == "" {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
ret, err := strconv.Atoi(val)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("invalid %s value %q: must be an integer", key, val)
|
|
|
|
|
}
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|