Compare commits
4 Commits
master
...
keyFromArg
Author | SHA1 | Date |
---|---|---|
Dan Ballard | 77794be7db | |
Dan Ballard | 08433db912 | |
Dan Ballard | a6ef43c637 | |
Dan Ballard | c079f0dace |
|
@ -36,6 +36,6 @@ func (ebs *EchoBotService) OnChatMessage(oc *goricochet.OpenConnection, channelI
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ricochetService := new(EchoBotService)
|
ricochetService := new(EchoBotService)
|
||||||
ricochetService.Init("./private_key")
|
ricochetService.InitFromKeyFile("./private_key")
|
||||||
ricochetService.Listen(ricochetService, 12345)
|
ricochetService.Listen(ricochetService, 12345)
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,21 +22,26 @@ type StandardRicochetService struct {
|
||||||
|
|
||||||
// Init initializes a StandardRicochetService with the cryptographic key given
|
// Init initializes a StandardRicochetService with the cryptographic key given
|
||||||
// by filename.
|
// by filename.
|
||||||
func (srs *StandardRicochetService) Init(filename string) error {
|
func (srs *StandardRicochetService) InitFromKeyFile(filename string) error {
|
||||||
srs.ricochet = new(Ricochet)
|
|
||||||
srs.ricochet.Init()
|
|
||||||
|
|
||||||
pemData, err := ioutil.ReadFile(filename)
|
pemData, err := ioutil.ReadFile(filename)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Could not setup ricochet service: could not read private key")
|
return errors.New("Could not setup ricochet service: could not read private key")
|
||||||
}
|
}
|
||||||
|
return srs.InitFromKey(pemData)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initializes a StandardRicochetService with the cryptographic key
|
||||||
|
func (srs *StandardRicochetService) InitFromKey(pemData []byte) error {
|
||||||
|
srs.ricochet = new(Ricochet)
|
||||||
|
srs.ricochet.Init()
|
||||||
|
|
||||||
block, _ := pem.Decode(pemData)
|
block, _ := pem.Decode(pemData)
|
||||||
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
||||||
return errors.New("Could not setup ricochet service: no valid PEM data found")
|
return errors.New("Could not setup ricochet service: no valid PEM data found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
srs.privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
|
srs.privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Could not setup ricochet service: could not parse private key")
|
return errors.New("Could not setup ricochet service: could not parse private key")
|
||||||
|
@ -180,3 +185,8 @@ func (srs *StandardRicochetService) OnBadUsageError(oc *OpenConnection, channelI
|
||||||
func (srs *StandardRicochetService) OnFailedError(oc *OpenConnection, channelID int32) {
|
func (srs *StandardRicochetService) OnFailedError(oc *OpenConnection, channelID int32) {
|
||||||
oc.RejectOpenChannel(channelID, "FailedError")
|
oc.RejectOpenChannel(channelID, "FailedError")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RGetServerHostname returns the generated tor hostname from the private key
|
||||||
|
func (srs *StandardRicochetService) GetServerHostname() string {
|
||||||
|
return srs.serverHostname
|
||||||
|
}
|
|
@ -77,7 +77,7 @@ func (ts *TestBadUsageService) IsKnownContact(hostname string) bool {
|
||||||
|
|
||||||
func TestBadUsageServer(t *testing.T) {
|
func TestBadUsageServer(t *testing.T) {
|
||||||
ricochetService := new(TestBadUsageService)
|
ricochetService := new(TestBadUsageService)
|
||||||
err := ricochetService.Init("./private_key")
|
err := ricochetService.InitFromKeyFile("./private_key")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not initate ricochet service: %v", err)
|
t.Errorf("Could not initate ricochet service: %v", err)
|
||||||
|
@ -88,7 +88,7 @@ func TestBadUsageServer(t *testing.T) {
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
|
|
||||||
ricochetService2 := new(TestBadUsageService)
|
ricochetService2 := new(TestBadUsageService)
|
||||||
err = ricochetService2.Init("./private_key")
|
err = ricochetService2.InitFromKeyFile("./private_key")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not initate ricochet service: %v", err)
|
t.Errorf("Could not initate ricochet service: %v", err)
|
||||||
|
|
|
@ -55,7 +55,7 @@ func (ts *TestService) IsKnownContact(hostname string) bool {
|
||||||
|
|
||||||
func TestServer(t *testing.T) {
|
func TestServer(t *testing.T) {
|
||||||
ricochetService := new(TestService)
|
ricochetService := new(TestService)
|
||||||
err := ricochetService.Init("./private_key")
|
err := ricochetService.InitFromKeyFile("./private_key")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not initate ricochet service: %v", err)
|
t.Errorf("Could not initate ricochet service: %v", err)
|
||||||
|
@ -66,7 +66,7 @@ func TestServer(t *testing.T) {
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
|
|
||||||
ricochetService2 := new(TestService)
|
ricochetService2 := new(TestService)
|
||||||
err = ricochetService2.Init("./private_key")
|
err = ricochetService2.InitFromKeyFile("./private_key")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not initate ricochet service: %v", err)
|
t.Errorf("Could not initate ricochet service: %v", err)
|
||||||
|
@ -87,7 +87,7 @@ func TestServer(t *testing.T) {
|
||||||
|
|
||||||
func TestServerInvalidKey(t *testing.T) {
|
func TestServerInvalidKey(t *testing.T) {
|
||||||
ricochetService := new(TestService)
|
ricochetService := new(TestService)
|
||||||
err := ricochetService.Init("./private_key.does.not.exist")
|
err := ricochetService.InitFromKeyFile("./private_key.does.not.exist")
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Should not have initate ricochet service, private key should not exist")
|
t.Errorf("Should not have initate ricochet service, private key should not exist")
|
||||||
|
@ -96,7 +96,7 @@ func TestServerInvalidKey(t *testing.T) {
|
||||||
|
|
||||||
func TestServerCouldNotConnect(t *testing.T) {
|
func TestServerCouldNotConnect(t *testing.T) {
|
||||||
ricochetService := new(TestService)
|
ricochetService := new(TestService)
|
||||||
err := ricochetService.Init("./private_key")
|
err := ricochetService.InitFromKeyFile("./private_key")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not initate ricochet service: %v", err)
|
t.Errorf("Could not initate ricochet service: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -105,3 +105,14 @@ func TestServerCouldNotConnect(t *testing.T) {
|
||||||
t.Errorf("Should not have been been able to connect to 127.0.0.1:65535|kwke2hntvyfqm7dr")
|
t.Errorf("Should not have been been able to connect to 127.0.0.1:65535|kwke2hntvyfqm7dr")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetServerHostname(t *testing.T) {
|
||||||
|
ricochetService := new(TestService)
|
||||||
|
err := ricochetService.InitFromKeyFile("./private_key")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Could not initate ricochet service: %v", err)
|
||||||
|
}
|
||||||
|
if ricochetService.GetServerHostname() != "kwke2hntvyfqm7dr" {
|
||||||
|
t.Errorf("GetServerHostname did not return expected 'kwke2hntvyfqm7dr'")
|
||||||
|
}
|
||||||
|
}
|
|
@ -32,7 +32,7 @@ func (ts *TestUnauthorizedService) OnFailedChannelOpen(oc *OpenConnection, chann
|
||||||
|
|
||||||
func TestUnauthorizedClientReject(t *testing.T) {
|
func TestUnauthorizedClientReject(t *testing.T) {
|
||||||
ricochetService := new(TestService)
|
ricochetService := new(TestService)
|
||||||
err := ricochetService.Init("./private_key")
|
err := ricochetService.InitFromKeyFile("./private_key")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not initate ricochet service: %v", err)
|
t.Errorf("Could not initate ricochet service: %v", err)
|
||||||
|
@ -43,7 +43,7 @@ func TestUnauthorizedClientReject(t *testing.T) {
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
|
|
||||||
ricochetService2 := new(TestUnauthorizedService)
|
ricochetService2 := new(TestUnauthorizedService)
|
||||||
err = ricochetService2.Init("./private_key")
|
err = ricochetService2.InitFromKeyFile("./private_key")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not initate ricochet service: %v", err)
|
t.Errorf("Could not initate ricochet service: %v", err)
|
||||||
|
|
|
@ -29,7 +29,7 @@ func (ts *TestUnknownContactService) IsKnownContact(hostname string) bool {
|
||||||
|
|
||||||
func TestUnknownContactServer(t *testing.T) {
|
func TestUnknownContactServer(t *testing.T) {
|
||||||
ricochetService := new(StandardRicochetService)
|
ricochetService := new(StandardRicochetService)
|
||||||
err := ricochetService.Init("./private_key")
|
err := ricochetService.InitFromKeyFile("./private_key")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not initate ricochet service: %v", err)
|
t.Errorf("Could not initate ricochet service: %v", err)
|
||||||
|
@ -40,7 +40,7 @@ func TestUnknownContactServer(t *testing.T) {
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
|
|
||||||
ricochetService2 := new(TestUnknownContactService)
|
ricochetService2 := new(TestUnknownContactService)
|
||||||
err = ricochetService2.Init("./private_key")
|
err = ricochetService2.InitFromKeyFile("./private_key")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Could not initate ricochet service: %v", err)
|
t.Errorf("Could not initate ricochet service: %v", err)
|
||||||
|
|
Loading…
Reference in New Issue