webntp

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2019 License: MIT Imports: 25 Imported by: 0

README

Build Status GoDoc

WebNTP

WebNTP is NTP(-like) service via HTTP/WebSocket.

Synopsis

First, go get and start the WebNTP Server.

$ go get github.com/shogo82148/go-webntp/cmd/webntp
$ webntp -serve :8080

Sync with the server via HTTP.

$ webntp http://localhost:8080/
server http://localhost:8080/, offset -0.000066, delay 0.001453
2017-03-11 18:25:10.905049427 +0900 JST, server http://localhost:8080/, offset -0.000066

Sync with the server via WebSocket.

$ webntp ws://localhost:8080/
server ws://localhost:8080/, offset -0.000013, delay 0.000288
2017-03-11 18:25:36.668531757 +0900 JST, server ws://localhost:8080/, offset -0.000013

NICT(National Institute of Information and Communications Technology) provides WebNTP-compatible API.

$ webntp https://ntp-a1.nict.go.jp/cgi-bin/json
server https://ntp-a1.nict.go.jp/cgi-bin/json, offset -0.006376, delay 0.024411
2017-03-11 16:08:06.150393313 +0900 JST, server https://ntp-a1.nict.go.jp/cgi-bin/json, offset -0.006376

Shared Memory support for ntpd

Add a new server to your ntpd.conf.

server 127.127.28.2 noselect
fudge 127.127.28.2 refid PYTH stratum 10

Run WebNTP with -shm 2 option.

$ webntp -p 1 -shm 2 https://ntp-a1.nict.go.jp/cgi-bin/json https://ntp-b1.nict.go.jp/cgi-bin/json
server https://ntp-a1.nict.go.jp/cgi-bin/json, offset -0.003258, delay 0.018910
server https://ntp-b1.nict.go.jp/cgi-bin/json, offset -0.003570, delay 0.021652

ntpd starts syncing with WebNTP.

$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 SHM(2)          .PYTH.          10 l    2   64   17    0.000   -3.331   0.384
*ntp-a2.nict.go. .NICT.           1 u   58   64   37   10.280    1.494   2.028

Usage

$ webntp --help
  -allow-cross-origin
    	allow cross origin request
  -help
    	show help
  -leap-second-path string
    	path for leap-seconds.list cache (default "leap-seconds.list")
  -leap-second-url string
    	url for leap-seconds.list (default "https://www.ietf.org/timezones/data/leap-seconds.list")
  -p int
    	Specify the number of samples (default 4)
  -serve string
    	server host name
  -shm uint
    	ntpd shared-memory-segment

Protocol

WebNTP returns UNIX timestamp by JSON. See the document of http/https service by NICT (the content is written in japanese).

$ curl -s http://localhost:8080/?1489217288.328757 | jq .
{
  "id": "localhost:8080",
  "it": 1489217288.328757,
  "st": 1489224472.995564,
  "time": 1489224472.995564,
  "leap": 36,
  "next": 1483228800,
  "step": 1
}

License

This software is released under the MIT License, see LICENSE.

Documentation

Index

Constants

View Source
const Subprotocol = "webntp.shogo82148.com"

Subprotocol is a subprotocol name for websocket.

View Source
const Version string = "0.0.1"

Version is the version of the package.

Variables

View Source
var DefaultDialer = &websocket.Dialer{
	Proxy:        http.ProxyFromEnvironment,
	Subprotocols: []string{Subprotocol},
}

DefaultDialer is a dialer for webntp.

Functions

This section is empty.

Types

type Client

type Client struct {
	HTTPClient *http.Client
	Dialer     *websocket.Dialer
}

Client is a webntp client.

func (*Client) Get

func (c *Client) Get(ctx context.Context, uri string) (Result, error)

Get gets synchronization information.

func (*Client) GetMulti

func (c *Client) GetMulti(ctx context.Context, uri string, samples int) (Result, error)

GetMulti gets synchronization information. It improve accuracy by calling the Get method many times.

type LeapSecond

type LeapSecond struct {
	// At is the time to insert/delete a leap second.
	At time.Time

	// Leap is offset from TAI to UTC. (**after** LeapSecond.Time)
	Leap int

	// Step describes next leap second is insertion or deletion.
	// +1 is insertion, -1 is deletion.
	Step int
}

LeapSecond is information for leap-seconds

type LeapSecondsList

type LeapSecondsList struct {
	LeapSeconds []LeapSecond
	UpdateAt    time.Time
	ExpireAt    time.Time
}

LeapSecondsList is the contents of leap-second.list.

func ParseLeapSecondsList

func ParseLeapSecondsList(r io.Reader) (*LeapSecondsList, error)

ParseLeapSecondsList parses leap-second.list.

type Response

type Response struct {
	// ID is the id of the web-ntp server.
	ID string `json:"id"`

	// InitiateTime is the time that the request has been sent.
	InitiateTime Timestamp `json:"it"`

	// SendTime is the time that the response has been sent.
	SendTime Timestamp `json:"st"`

	// Time is a same as SendTime.
	// It is for htptime compatibility.
	Time Timestamp `json:"time"`

	// Leap is from TAI to UTC. (**before** Response.Next)
	Leap int `json:"leap"`

	// Next is the time of next or last leap second event.
	Next Timestamp `json:"next"`

	// Step describes next or last leap second is insertion or deletion.
	// +1 is insertion, -1 is deletion.
	Step int `json:"step"`
}

Response is a response from webntp server.

type Result

type Result struct {
	Offset    time.Duration
	Delay     time.Duration
	NextLeap  time.Time
	TAIOffset time.Duration
	Step      int
}

Result is the result of synchronization.

type Server

type Server struct {
	Upgrader *websocket.Upgrader

	// path for leap-seconds.list cache
	LeapSecondsPath string

	// url for leap-seconds.list
	LeapSecondsURL string
	// contains filtered or unexported fields
}

Server is a webntp server.

func (*Server) Close

func (s *Server) Close() error

Close closes the server.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request)

func (*Server) Start

func (s *Server) Start() error

Start starts fetching leap-seconds.list

type Timestamp

type Timestamp time.Time

Timestamp is posix unix timestamp.

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON converts the timestamp to JSON number. The number is unix timestamp.

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(b []byte) error

UnmarshalJSON converts JSON number to a timestamp. The number is unix timestamp.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL