implement encoding Marshal/Unmarshal
This commit is contained in:
@@ -61,12 +61,15 @@ func su3Action(c *cli.Context) {
|
|||||||
seeds, err := reseeder.Seeds(reseed.Peer("127.0.0.1"))
|
seeds, err := reseeder.Seeds(reseed.Peer("127.0.0.1"))
|
||||||
if nil != err {
|
if nil != err {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// create an SU3 from the seed
|
// create an SU3 from the seed
|
||||||
su3File, err := reseeder.CreateSu3(seeds)
|
su3File, err := reseeder.CreateSu3(seeds)
|
||||||
|
|
||||||
//write the file to disk
|
//write the file to disk
|
||||||
ioutil.WriteFile("i2pseeds.su3", su3File.Bytes(), 0777)
|
data, err := su3File.MarshalBinary()
|
||||||
|
if nil != err {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
ioutil.WriteFile("i2pseeds.su3", data, 0777)
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/MDrollette/go-i2p/reseed"
|
"github.com/MDrollette/go-i2p/reseed"
|
||||||
"github.com/MDrollette/go-i2p/su3"
|
"github.com/MDrollette/go-i2p/su3"
|
||||||
@@ -20,14 +20,13 @@ func NewSu3VerifyCommand() cli.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func su3VerifyAction(c *cli.Context) {
|
func su3VerifyAction(c *cli.Context) {
|
||||||
file, err := os.Open(c.Args().Get(0))
|
su3File := su3.Su3File{}
|
||||||
if err != nil {
|
|
||||||
|
data, err := ioutil.ReadFile(c.Args().Get(0))
|
||||||
|
if nil != err {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
if err := su3File.UnmarshalBinary(data); err != nil {
|
||||||
|
|
||||||
su3File, err := su3.Parse(file)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,6 +3,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -89,11 +90,14 @@ func validate(responses chan *http.Response) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
su3File, err := su3.Parse(resp.Body)
|
su3File := su3.Su3File{}
|
||||||
if err != nil {
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if nil != err {
|
||||||
|
fmt.Println("Invalid: Unable to parse SU3 file:", err)
|
||||||
|
}
|
||||||
|
if err := su3File.UnmarshalBinary(data); err != nil {
|
||||||
fmt.Println("Invalid: Unable to parse SU3 file:", err)
|
fmt.Println("Invalid: Unable to parse SU3 file:", err)
|
||||||
}
|
}
|
||||||
resp.Body.Close()
|
|
||||||
|
|
||||||
cert, err := ks.ReseederCertificate(su3File.SignerId)
|
cert, err := ks.ReseederCertificate(su3File.SignerId)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
@@ -118,7 +118,12 @@ func (rs *ReseederImpl) rebuild() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
newSu3s[i] = gs.Bytes()
|
data, err := gs.MarshalBinary()
|
||||||
|
if nil != err {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
newSu3s[i] = data
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.su3s <- newSu3s
|
rs.su3s <- newSu3s
|
||||||
|
91
su3/su3.go
91
su3/su3.go
@@ -8,7 +8,6 @@ import (
|
|||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -145,7 +144,7 @@ func (s *Su3File) BodyBytes() []byte {
|
|||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Su3File) Bytes() []byte {
|
func (s *Su3File) MarshalBinary() ([]byte, error) {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
// write the body
|
// write the body
|
||||||
@@ -153,7 +152,50 @@ func (s *Su3File) Bytes() []byte {
|
|||||||
// append the signature
|
// append the signature
|
||||||
binary.Write(buf, binary.BigEndian, s.Signature)
|
binary.Write(buf, binary.BigEndian, s.Signature)
|
||||||
|
|
||||||
return buf.Bytes()
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Su3File) UnmarshalBinary(data []byte) error {
|
||||||
|
var (
|
||||||
|
r = bytes.NewReader(data)
|
||||||
|
|
||||||
|
magic = MAGIC_BYTES
|
||||||
|
skip [1]byte
|
||||||
|
bigSkip [12]byte
|
||||||
|
|
||||||
|
signatureLength uint16
|
||||||
|
versionLength uint8
|
||||||
|
signerIdLength uint8
|
||||||
|
contentLength uint64
|
||||||
|
)
|
||||||
|
|
||||||
|
binary.Read(r, binary.BigEndian, &magic)
|
||||||
|
binary.Read(r, binary.BigEndian, &skip)
|
||||||
|
binary.Read(r, binary.BigEndian, &s.Format)
|
||||||
|
binary.Read(r, binary.BigEndian, &s.SignatureType)
|
||||||
|
binary.Read(r, binary.BigEndian, &signatureLength)
|
||||||
|
binary.Read(r, binary.BigEndian, &skip)
|
||||||
|
binary.Read(r, binary.BigEndian, &versionLength)
|
||||||
|
binary.Read(r, binary.BigEndian, &skip)
|
||||||
|
binary.Read(r, binary.BigEndian, &signerIdLength)
|
||||||
|
binary.Read(r, binary.BigEndian, &contentLength)
|
||||||
|
binary.Read(r, binary.BigEndian, &skip)
|
||||||
|
binary.Read(r, binary.BigEndian, &s.FileType)
|
||||||
|
binary.Read(r, binary.BigEndian, &skip)
|
||||||
|
binary.Read(r, binary.BigEndian, &s.ContentType)
|
||||||
|
binary.Read(r, binary.BigEndian, &bigSkip)
|
||||||
|
|
||||||
|
s.Version = make([]byte, versionLength)
|
||||||
|
s.SignerId = make([]byte, signerIdLength)
|
||||||
|
s.Content = make([]byte, contentLength)
|
||||||
|
s.Signature = make([]byte, signatureLength)
|
||||||
|
|
||||||
|
binary.Read(r, binary.BigEndian, &s.Version)
|
||||||
|
binary.Read(r, binary.BigEndian, &s.SignerId)
|
||||||
|
binary.Read(r, binary.BigEndian, &s.Content)
|
||||||
|
binary.Read(r, binary.BigEndian, &s.Signature)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Su3File) VerifySignature(cert *x509.Certificate) error {
|
func (s *Su3File) VerifySignature(cert *x509.Certificate) error {
|
||||||
@@ -200,46 +242,3 @@ func (s *Su3File) String() string {
|
|||||||
|
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse(r io.Reader) (*Su3File, error) {
|
|
||||||
var (
|
|
||||||
s = Su3File{}
|
|
||||||
|
|
||||||
magic = MAGIC_BYTES
|
|
||||||
skip [1]byte
|
|
||||||
bigSkip [12]byte
|
|
||||||
|
|
||||||
signatureLength uint16
|
|
||||||
versionLength uint8
|
|
||||||
signerIdLength uint8
|
|
||||||
contentLength uint64
|
|
||||||
)
|
|
||||||
|
|
||||||
binary.Read(r, binary.BigEndian, &magic)
|
|
||||||
binary.Read(r, binary.BigEndian, &skip)
|
|
||||||
binary.Read(r, binary.BigEndian, &s.Format)
|
|
||||||
binary.Read(r, binary.BigEndian, &s.SignatureType)
|
|
||||||
binary.Read(r, binary.BigEndian, &signatureLength)
|
|
||||||
binary.Read(r, binary.BigEndian, &skip)
|
|
||||||
binary.Read(r, binary.BigEndian, &versionLength)
|
|
||||||
binary.Read(r, binary.BigEndian, &skip)
|
|
||||||
binary.Read(r, binary.BigEndian, &signerIdLength)
|
|
||||||
binary.Read(r, binary.BigEndian, &contentLength)
|
|
||||||
binary.Read(r, binary.BigEndian, &skip)
|
|
||||||
binary.Read(r, binary.BigEndian, &s.FileType)
|
|
||||||
binary.Read(r, binary.BigEndian, &skip)
|
|
||||||
binary.Read(r, binary.BigEndian, &s.ContentType)
|
|
||||||
binary.Read(r, binary.BigEndian, &bigSkip)
|
|
||||||
|
|
||||||
s.Version = make([]byte, versionLength)
|
|
||||||
s.SignerId = make([]byte, signerIdLength)
|
|
||||||
s.Content = make([]byte, contentLength)
|
|
||||||
s.Signature = make([]byte, signatureLength)
|
|
||||||
|
|
||||||
binary.Read(r, binary.BigEndian, &s.Version)
|
|
||||||
binary.Read(r, binary.BigEndian, &s.SignerId)
|
|
||||||
binary.Read(r, binary.BigEndian, &s.Content)
|
|
||||||
binary.Read(r, binary.BigEndian, &s.Signature)
|
|
||||||
|
|
||||||
return &s, nil
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user