Cameradar v4 (#212)
* Refactor of cameradar library * Old unit tests updated & improved. New unit tests inc * Update documentation & issue template * Update dependencies * Update TravisCI build script to reflect argument change * Remove outdated contributing guide * Update README with more examples and remove part on library * Add second camera to Travis build script & improve error detection * Fix typo in travis script & add missing image to readme * Remember that travis uses bash syntax not fish * Use relative paths for images in the README
This commit is contained in:
committed by
GitHub
parent
2e49587cc2
commit
212ac2f0d5
+43
-30
@@ -1,14 +1,13 @@
|
||||
package cmrdr
|
||||
package cameradar
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var fs fileSystem = osFS{}
|
||||
@@ -32,48 +31,51 @@ type osFS struct{}
|
||||
func (osFS) Open(name string) (file, error) { return os.Open(name) }
|
||||
func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
|
||||
|
||||
// LoadCredentials opens a dictionary file and returns its contents as a Credentials structure
|
||||
func LoadCredentials(path string) (Credentials, error) {
|
||||
var creds Credentials
|
||||
// LoadCredentials opens a dictionary file and returns its contents as a Credentials structure.
|
||||
func (s *Scanner) LoadCredentials() error {
|
||||
s.term.Debugf("Loading credentials dictionary from path %q\n", s.credentialDictionaryPath)
|
||||
|
||||
// Open & Read XML file
|
||||
content, err := ioutil.ReadFile(path)
|
||||
// Open & Read XML file.
|
||||
content, err := ioutil.ReadFile(s.credentialDictionaryPath)
|
||||
if err != nil {
|
||||
return creds, errors.Wrap(err, "could not read credentials dictionary file at "+path+":")
|
||||
return fmt.Errorf("could not read credentials dictionary file at %q: %v", s.credentialDictionaryPath, err)
|
||||
}
|
||||
|
||||
// Unmarshal content of JSON file into data structure
|
||||
err = json.Unmarshal(content, &creds)
|
||||
// Unmarshal content of JSON file into data structure.
|
||||
err = json.Unmarshal(content, &s.credentials)
|
||||
if err != nil {
|
||||
return creds, err
|
||||
return fmt.Errorf("unable to unmarshal dictionary contents: %v", err)
|
||||
}
|
||||
|
||||
return creds, nil
|
||||
s.term.Debugf("Loaded %d usernames and %d passwords\n", len(s.credentials.Usernames), len(s.credentials.Passwords))
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadRoutes opens a dictionary file and returns its contents as a Routes structure
|
||||
func LoadRoutes(path string) (Routes, error) {
|
||||
file, err := os.Open(path)
|
||||
// LoadRoutes opens a dictionary file and returns its contents as a Routes structure.
|
||||
func (s *Scanner) LoadRoutes() error {
|
||||
s.term.Debugf("Loading routes dictionary from path %q\n", s.routeDictionaryPath)
|
||||
|
||||
file, err := os.Open(s.routeDictionaryPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return fmt.Errorf("unable to open dictionary: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var routes Routes
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
for scanner.Scan() {
|
||||
routes = append(routes, scanner.Text())
|
||||
s.routes = append(s.routes, scanner.Text())
|
||||
}
|
||||
|
||||
return routes, scanner.Err()
|
||||
s.term.Debugf("Loaded %d routes\n", len(s.routes))
|
||||
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
// ParseCredentialsFromString parses a dictionary string and returns its contents as a Credentials structure
|
||||
// ParseCredentialsFromString parses a dictionary string and returns its contents as a Credentials structure.
|
||||
func ParseCredentialsFromString(content string) (Credentials, error) {
|
||||
var creds Credentials
|
||||
|
||||
// Unmarshal content of JSON file into data structure
|
||||
// Unmarshal content of JSON file into data structure.
|
||||
err := json.Unmarshal([]byte(content), &creds)
|
||||
if err != nil {
|
||||
return creds, err
|
||||
@@ -82,28 +84,39 @@ func ParseCredentialsFromString(content string) (Credentials, error) {
|
||||
return creds, nil
|
||||
}
|
||||
|
||||
// ParseRoutesFromString parses a dictionary string and returns its contents as a Routes structure
|
||||
// ParseRoutesFromString parses a dictionary string and returns its contents as a Routes structure.
|
||||
func ParseRoutesFromString(content string) Routes {
|
||||
return strings.Split(content, "\n")
|
||||
}
|
||||
|
||||
// ParseTargetsFile parses an input file containing hosts to targets
|
||||
func ParseTargetsFile(path string) ([]string, error) {
|
||||
// LoadTargets parses the file containing hosts to targets, if the targets are
|
||||
// just set to a file name.
|
||||
func (s *Scanner) LoadTargets() error {
|
||||
if len(s.targets) != 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
path := s.targets[0]
|
||||
|
||||
_, err := fs.Stat(path)
|
||||
if err != nil {
|
||||
return []string{path}, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
file, err := fs.Open(path)
|
||||
if err != nil {
|
||||
return []string{path}, err
|
||||
return fmt.Errorf("unable to open targets file %q: %v", path, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
bytes, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return []string{path}, err
|
||||
return fmt.Errorf("unable to read targets file %q: %v", path, err)
|
||||
}
|
||||
|
||||
return strings.Split(string(bytes), "\n"), nil
|
||||
s.targets = strings.Split(string(bytes), "\n")
|
||||
|
||||
s.term.Debugf("Successfylly parsed targets file with %d entries", len(s.targets))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user