mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-05 16:02:17 +00:00
Why: Ut is HTTPS to an operator-internal IPv4 XCAP host, not SIP. IMS IPv6-only could not reach it, and CS-only devices still opened the page and waited on 502. Why this works: Vodafone UK starts a second SWu session with APN xcap and omits INITIAL_CONTACT. Errors no longer leak the XCAP URL. The sidebar drops 呼叫设置; /ut redirects home. Remaining: the xcap PDN gets IPv4, but TCP to the XCAP address still times out.
231 lines
6.4 KiB
Go
231 lines
6.4 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/iniwex5/vowifi-go/xcap"
|
|
)
|
|
|
|
var (
|
|
errUtUnavailable = errors.New("Ut/XCAP is unavailable")
|
|
errUtXCAPPDN = errors.New("XCAP PDN is not established")
|
|
errUtIdentity = errors.New("IMS public identity is not registered")
|
|
errUtManyChanges = errors.New("one Ut request may change only one service")
|
|
)
|
|
|
|
type utIdentity struct {
|
|
XUI string
|
|
Fallback []string
|
|
}
|
|
|
|
type utClientFunc func(string) (*xcap.Client, utIdentity, error)
|
|
|
|
type utView struct {
|
|
XUI string `json:"xui"`
|
|
ETag string `json:"etag"`
|
|
CommunicationDiversion utToggle `json:"communication_diversion"`
|
|
IdentityRestriction utIdentityView `json:"identity_restriction"`
|
|
IncomingBarring utToggle `json:"incoming_barring"`
|
|
OutgoingBarring utToggle `json:"outgoing_barring"`
|
|
}
|
|
|
|
type utToggle struct {
|
|
Active bool `json:"active"`
|
|
Target string `json:"target,omitempty"`
|
|
}
|
|
|
|
type utIdentityView struct {
|
|
Active bool `json:"active"`
|
|
Restricted bool `json:"restricted"`
|
|
}
|
|
|
|
type utPatchRequest struct {
|
|
ETag string `json:"etag"`
|
|
CommunicationDiversion *utToggle `json:"communication_diversion"`
|
|
IdentityRestriction *utIdentityView `json:"identity_restriction"`
|
|
IncomingBarring *utToggle `json:"incoming_barring"`
|
|
OutgoingBarring *utToggle `json:"outgoing_barring"`
|
|
}
|
|
|
|
func (s *Server) registerUtRoutes(api *gin.RouterGroup) {
|
|
api.GET("/devices/:device_id/ut/simservs", s.handleUtGet)
|
|
api.PUT("/devices/:device_id/ut/simservs", s.handleUtPut)
|
|
}
|
|
|
|
func (s *Server) handleUtGet(c *gin.Context) {
|
|
client, ident, err := s.lookupUtClient(deviceIDParam(c))
|
|
if err != nil {
|
|
writeUtError(c, err)
|
|
return
|
|
}
|
|
doc, err := client.Get(c.Request.Context(), ident.XUI, ident.Fallback)
|
|
if err != nil {
|
|
writeUtError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, utViewFromDocument(doc))
|
|
}
|
|
|
|
func (s *Server) handleUtPut(c *gin.Context) {
|
|
var req utPatchRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": "invalid JSON"})
|
|
return
|
|
}
|
|
if n := req.changeCount(); n != 1 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": errUtManyChanges.Error()})
|
|
return
|
|
}
|
|
client, ident, err := s.lookupUtClient(deviceIDParam(c))
|
|
if err != nil {
|
|
writeUtError(c, err)
|
|
return
|
|
}
|
|
doc, err := client.Get(c.Request.Context(), ident.XUI, ident.Fallback)
|
|
if err != nil {
|
|
writeUtError(c, err)
|
|
return
|
|
}
|
|
if strings.TrimSpace(req.ETag) == "" || req.ETag != doc.ETag {
|
|
c.JSON(http.StatusPreconditionFailed, gin.H{"status": "error", "message": xcap.ErrPrecondition.Error()})
|
|
return
|
|
}
|
|
req.apply(&doc)
|
|
saved, err := client.Put(c.Request.Context(), doc)
|
|
if err != nil {
|
|
writeUtError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, utViewFromDocument(saved))
|
|
}
|
|
|
|
func (s *Server) lookupUtClient(deviceID string) (*xcap.Client, utIdentity, error) {
|
|
deviceID = strings.TrimSpace(deviceID)
|
|
if deviceID == "" {
|
|
return nil, utIdentity{}, errUtIdentity
|
|
}
|
|
if s != nil && s.utClient != nil {
|
|
return s.utClient(deviceID)
|
|
}
|
|
if s == nil || s.pool == nil {
|
|
return nil, utIdentity{}, errUtXCAPPDN
|
|
}
|
|
inst := s.pool.GetVoWiFiAppForDevice(deviceID)
|
|
if inst == nil {
|
|
return nil, utIdentity{}, errUtXCAPPDN
|
|
}
|
|
access, err := inst.UtAccess()
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "public identity") {
|
|
return nil, utIdentity{}, errUtIdentity
|
|
}
|
|
return nil, utIdentity{}, errUtXCAPPDN
|
|
}
|
|
return access.Client, utIdentity{XUI: access.XUI, Fallback: access.More}, nil
|
|
}
|
|
|
|
func (req utPatchRequest) changeCount() int {
|
|
n := 0
|
|
if req.CommunicationDiversion != nil {
|
|
n++
|
|
}
|
|
if req.IdentityRestriction != nil {
|
|
n++
|
|
}
|
|
if req.IncomingBarring != nil {
|
|
n++
|
|
}
|
|
if req.OutgoingBarring != nil {
|
|
n++
|
|
}
|
|
return n
|
|
}
|
|
|
|
func (req utPatchRequest) apply(doc *xcap.Document) {
|
|
if req.CommunicationDiversion != nil {
|
|
doc.SetCFU(req.CommunicationDiversion.Active, req.CommunicationDiversion.Target)
|
|
}
|
|
if req.IdentityRestriction != nil {
|
|
doc.SetOIR(req.IdentityRestriction.Active, req.IdentityRestriction.Restricted)
|
|
}
|
|
if req.IncomingBarring != nil || req.OutgoingBarring != nil {
|
|
in, out := false, false
|
|
if doc.ICB != nil {
|
|
in = doc.ICB.Active
|
|
}
|
|
if doc.OCB != nil {
|
|
out = doc.OCB.Active
|
|
}
|
|
if req.IncomingBarring != nil {
|
|
in = req.IncomingBarring.Active
|
|
}
|
|
if req.OutgoingBarring != nil {
|
|
out = req.OutgoingBarring.Active
|
|
}
|
|
doc.SetBarring(in, out)
|
|
}
|
|
}
|
|
|
|
func utViewFromDocument(doc xcap.Document) utView {
|
|
view := utView{XUI: doc.XUI, ETag: doc.ETag}
|
|
if doc.CDIV != nil {
|
|
view.CommunicationDiversion = utToggle{Active: doc.CDIV.Active, Target: doc.CFUTarget()}
|
|
}
|
|
if doc.OIR != nil {
|
|
view.IdentityRestriction = utIdentityView{Active: doc.OIR.Active, Restricted: doc.IdentityRestricted()}
|
|
}
|
|
if doc.ICB != nil {
|
|
view.IncomingBarring = utToggle{Active: doc.ICB.Active}
|
|
}
|
|
if doc.OCB != nil {
|
|
view.OutgoingBarring = utToggle{Active: doc.OCB.Active}
|
|
}
|
|
return view
|
|
}
|
|
|
|
func writeUtError(c *gin.Context, err error) {
|
|
c.JSON(utStatus(err), gin.H{"status": "error", "message": utPublicMessage(err)})
|
|
}
|
|
|
|
func utPublicMessage(err error) string {
|
|
switch {
|
|
case errors.Is(err, xcap.ErrNotFound):
|
|
return "运营商没有这份补充业务文档"
|
|
case errors.Is(err, xcap.ErrPrecondition):
|
|
return "补充业务已被其他请求更新,请刷新后重试"
|
|
case errors.Is(err, errUtManyChanges):
|
|
return errUtManyChanges.Error()
|
|
case errors.Is(err, errUtIdentity):
|
|
return "IMS 未注册,无法读取补充业务"
|
|
case errors.Is(err, errUtXCAPPDN), errors.Is(err, errUtUnavailable):
|
|
return "XCAP 承载未建立"
|
|
case errors.Is(err, xcap.ErrUnavailable):
|
|
if strings.Contains(err.Error(), "timed out") {
|
|
return "运营商 Ut/XCAP 超时:补充业务服务器在运营商内网,当前 IMS 隧道连不上。"
|
|
}
|
|
return "运营商 Ut/XCAP 不可用"
|
|
default:
|
|
return "补充业务请求失败"
|
|
}
|
|
}
|
|
|
|
func utStatus(err error) int {
|
|
switch {
|
|
case errors.Is(err, xcap.ErrNotFound):
|
|
return http.StatusNotFound
|
|
case errors.Is(err, xcap.ErrPrecondition):
|
|
return http.StatusPreconditionFailed
|
|
case errors.Is(err, errUtManyChanges):
|
|
return http.StatusBadRequest
|
|
case errors.Is(err, errUtIdentity), errors.Is(err, errUtXCAPPDN), errors.Is(err, errUtUnavailable):
|
|
return http.StatusConflict
|
|
case errors.Is(err, xcap.ErrUnavailable):
|
|
return http.StatusBadGateway
|
|
default:
|
|
return http.StatusBadGateway
|
|
}
|
|
}
|