-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.go
102 lines (87 loc) · 2.46 KB
/
signature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package polysdk
import (
"encoding/json"
"fmt"
"sync/atomic"
"time"
"github.com/quanxiang-cloud/go-polysdk/internal/hash"
"github.com/quanxiang-cloud/go-polysdk/internal/polysign"
)
// Now return time that adjust with server.
func (c *PolyClient) Now() time.Time {
t := time.Now()
if adj := c.getTimeAdjust(); adj != 0 {
t = t.Add(adj)
}
return t
}
// SetTimeAdjustMS change local time adjust value to coordinate with server.
// NOTE: It never change local system clock.
func (c *PolyClient) SetTimeAdjustMS(adjust int64) {
timeAdjust := time.Duration(adjust) * time.Millisecond
c.setTimeAdjust(timeAdjust)
}
func (c *PolyClient) getTimeAdjust() time.Duration {
return time.Duration(atomic.LoadInt64(&c.timeAdjust))
}
func (c *PolyClient) setTimeAdjust(adjust time.Duration) {
fmt.Printf("SetTimeAdjustMS %s => %s\n", c.getTimeAdjust(), adjust)
timeAdjust := int64(adjust)
atomic.StoreInt64(&c.timeAdjust, timeAdjust)
}
// Timestamp get current timestamp of UTC
func (c *PolyClient) Timestamp(f string) string {
if f == "" {
f = "2006-01-02T15:04:05Z"
}
return c.Now().UTC().Format(f)
}
func (c *PolyClient) polyTimestamp() string {
return c.Now().Format(polysign.XHeaderPolySignTimestampFmt)
}
func systemNow() time.Time {
return time.Now()
}
// SyncServerClock auto adjust time with server
func (c *PolyClient) SyncServerClock() error {
uri := "/api/v1/gate/ping"
body := polysign.PingReq{
Rand: hash.ShortID(0),
PingTimestamp: systemNow().Format(polysign.PingTimestampFmt),
}
h := Header{}
h.Set(HeaderContentType, "application/json")
r, err := c.DoRequestAPI(uri, MethodPost, h, body)
if err != nil {
return err
}
var respBody struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data polysign.PingResp `json:"data"`
}
var d *polysign.PingResp = &respBody.Data
if err := json.Unmarshal(r.Body, &respBody); err != nil {
return err
}
ping, err := time.Parse(polysign.PingTimestampFmt, d.PingTimestamp)
if err != nil {
return err
}
pong, err := time.Parse(polysign.PingTimestampFmt, d.PongTimestamp)
if err != nil {
return err
}
now := systemNow()
netDelay := now.Sub(ping) / 10
adjust := pong.Add(netDelay).Sub(now)
c.setTimeAdjust(adjust)
// if true {
// println("adjust", adjust.String())
// println("netDelay", netDelay.String())
// println("ping", d.PingTimestamp, ping.String())
// println("pong", d.PongTimestamp, pong.String())
// println("now", now.String())
// }
return nil
}