Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function comments based on best practices from Effective Go #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions protocol/bitcoin_scanner.go
Expand Up @@ -75,19 +75,19 @@ type Packet struct {
payload []byte
}

// Bitcoin 256-bit hash
// Hash256: Bitcoin 256-bit hash
func Hash256(payload []byte) [32]byte {
h1 := sha256.Sum256(payload)
return sha256.Sum256(h1[:])
}

// Bitcoin P2P packet checksum
// Checksum: Bitcoin P2P packet checksum
func Checksum(payload []byte) []byte {
hash := Hash256(payload)
return hash[0:4]
}

// Read "compact size" varint.
// ReadCompactSize reads "compact size" varint.
// Return value and size of value read. The latter will be 0 on error,
// which happens if there was not enough space to read it.
func ReadCompactSize(input []byte) (uint64, int) {
Expand Down Expand Up @@ -116,7 +116,7 @@ func cstring(n []byte) string {
return string(n)
}

// Send P2P packet to connection
// SendPacket: Send P2P packet to connection
func SendPacket(conn net.Conn, msgstart []byte, pkt *Packet) error {
hdr := make([]byte, 24, 24)
copy(hdr[0:4], msgstart)
Expand All @@ -135,7 +135,7 @@ func SendPacket(conn net.Conn, msgstart []byte, pkt *Packet) error {
return nil
}

// Receive P2P packet from connection
// ReceivePacket: Receive P2P packet from connection
func ReceivePacket(conn net.Conn, msgstart []byte) (*Packet, error) {
var pkt Packet
hdr := make([]byte, 24, 24)
Expand Down Expand Up @@ -165,7 +165,7 @@ func ReceivePacket(conn net.Conn, msgstart []byte) (*Packet, error) {
return &pkt, nil
}

// Encode .onion address into 16-byte "IPv6" address used by Bitcoin P2P
// EncodeOnion: Encode .onion address into 16-byte "IPv6" address used by Bitcoin P2P
func EncodeOnion(onion string) ([]byte, error) {
r := regexp.MustCompile(`(\.|^)([a-z0-7]{16})\.onion$`)
onion_base := r.FindStringSubmatch(onion)
Expand All @@ -183,15 +183,15 @@ func EncodeOnion(onion string) ([]byte, error) {
return theiraddr, nil
}

// Extract .onion from 16-byte "IPv6" address used by Bitcoin P2P
// DecodeOnion: Extract .onion from 16-byte "IPv6" address used by Bitcoin P2P
func DecodeOnion(addr []byte) (string, error) {
if bytes.Equal(addr[0:len(AddrStartOnion)], AddrStartOnion) {
return strings.ToLower(base32.StdEncoding.EncodeToString(addr[len(AddrStartOnion):])) + ".onion", nil
}
return "", fmt.Errorf("Not an onion address")
}

// Build and send version message
// SendVersion: Build and send version message
func (rps *BitcoinProtocolScanner) SendVersion(conn net.Conn, osc *config.OnionScanConfig, hiddenService string) error {
// Most fields can be left at zero
payload := make([]byte, 80, 80) // static part of payload
Expand All @@ -213,7 +213,7 @@ func (rps *BitcoinProtocolScanner) SendVersion(conn net.Conn, osc *config.OnionS
return SendPacket(conn, rps.msgstart, &Packet{MSG_VERSION, payload})
}

// Handle incoming version message, and parse message payload into report
// HandleVersion handles incoming version message, and parse message payload into report
func (rps *BitcoinProtocolScanner) HandleVersion(conn net.Conn, osc *config.OnionScanConfig, report *report.BitcoinService, pkt *Packet) error {
report.ProtocolVersion = int(binary.LittleEndian.Uint32(pkt.payload[0:4]))
user_agent_length, sizesize := ReadCompactSize(pkt.payload[80:])
Expand All @@ -226,23 +226,23 @@ func (rps *BitcoinProtocolScanner) HandleVersion(conn net.Conn, osc *config.Onio
return nil
}

// Handle incoming verack message
// HandleVerAck handles incoming verack message
func (rps *BitcoinProtocolScanner) HandleVerAck(conn net.Conn, osc *config.OnionScanConfig, report *report.BitcoinService, pkt *Packet) error {
// This message has no content. However when receiving this message the
// version negotiation has been completed, and that other queries can be sent.
osc.LogInfo(fmt.Sprintf("Sending getaddr message"))
return SendPacket(conn, rps.msgstart, &Packet{MSG_GETADDR, []byte{}})
}

// Handle incoming ping message
// HandlePing handles incoming ping message
func (rps *BitcoinProtocolScanner) HandlePing(conn net.Conn, osc *config.OnionScanConfig, report *report.BitcoinService, pkt *Packet) error {
if len(pkt.payload) >= 8 { // Ping message with nonce, peer expects a pong
return SendPacket(conn, rps.msgstart, &Packet{MSG_PONG, pkt.payload[0:8]})
}
return nil
}

// Handle incoming addr message, and parse message payload into report
// HandleAddr handles incoming addr message, and parse message payload into report
func (rps *BitcoinProtocolScanner) HandleAddr(conn net.Conn, osc *config.OnionScanConfig, report *report.BitcoinService, pkt *Packet) error {
numaddr, sizesize := ReadCompactSize(pkt.payload)
if sizesize == 0 || numaddr > MAX_ADDR {
Expand All @@ -267,7 +267,7 @@ func (rps *BitcoinProtocolScanner) HandleAddr(conn net.Conn, osc *config.OnionSc
return nil
}

// Receive messages and handle them
// MessageLoop: Receive messages and handle them
func (rps *BitcoinProtocolScanner) MessageLoop(conn net.Conn, osc *config.OnionScanConfig, report *report.BitcoinService) error {
addrCount := 0
for addrCount < 2 {
Expand Down
2 changes: 1 addition & 1 deletion report/simple_report.go
Expand Up @@ -30,7 +30,7 @@ func (osr *SimpleReport) AddRisk(severity string, title string, description stri
osr.Risks = append(osr.Risks, Risk{severity, title, description, fix, items})
}

// Format as JSON
// Serialize: Format as JSON
func (osr *SimpleReport) Serialize() (string, error) {
report, err := json.Marshal(osr)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion utils/proxycheck.go
Expand Up @@ -17,7 +17,7 @@ const (
ProxyStatusTimeout
)

// Detect whether a proxy is connectable and is a Tor proxy
// CheckTorProxy: Detect whether a proxy is connectable and is a Tor proxy
func CheckTorProxy(proxyAddress string) ProxyStatus {
// A trick to do this without making an outward connection is,
// paradoxically, to try to open it as http.
Expand Down
4 changes: 2 additions & 2 deletions webui/webui.go
Expand Up @@ -61,7 +61,7 @@ type Table struct {
AltTitle string
}

// GetUserDefinedRow returns, from an initial relationship, a complete user
// GetUserDefinedTable returns, from an initial relationship, a complete user
// defined relationship row - in the order it is defined in the crawl config.
func (wui *WebUI) GetUserDefinedTable(rel crawldb.Relationship) (Table, error) {
log.Printf("Loading User Defined Relationship %s", rel.From)
Expand Down Expand Up @@ -169,7 +169,7 @@ func (wui *WebUI) Tag(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path, http.StatusFound)
}

// Delete tag implements the /delete-tag endpoint
// DeleteTag: Delete tag implements the /delete-tag endpoint
func (wui *WebUI) DeleteTag(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
Expand Down