Skip to content

Commit

Permalink
Merge branch 'feat/agent/demo'
Browse files Browse the repository at this point in the history
  • Loading branch information
cmj0121 committed Jul 14, 2023
2 parents ec57ae3 + 1279203 commit 317d0e3
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 468 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ help: # show this message
awk 'BEGIN {FS = ":.*?#"} {printf " %-18s %s\n", $$1, $$2}'

dist/%: cmd/%/main.go $(SRC)
mkdir -p $(@D)
@mkdir -p $(@D)
@go mod tidy
go build -ldflags="-s -w" -o $@ $<
11 changes: 7 additions & 4 deletions clotho.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/json"
"os"

"github.com/cmj0121/clotho/internal/demo"
"github.com/cmj0121/clotho/internal/github"
"github.com/cmj0121/clotho/internal/linkedin"

"github.com/alecthomas/kong"
"github.com/olekukonko/tablewriter"
Expand All @@ -26,7 +26,7 @@ type Clotho struct {
Verbose int `short:"v" group:"logger" xor:"verbose,quiet" type:"counter" help:"Show the verbose logger."`

Github *github.GitHub `cmd:"" help:"The GitHub user collector."`
LinkedIn *linkedin.LinkedIn `cmd:"" name:"linkedin" help:"The LinkedIn user collector."`
Demo *demo.Demo `cmd:"" name:"demo" help:"The Demo collector."`
}

// Create the new instance of the Clotho.
Expand All @@ -48,8 +48,8 @@ func (c *Clotho) Run() (exitcode int) {
switch sub := ctx.Command(); sub {
case "github <username>":
command = c.Github
case "linkedin <username>":
command = c.LinkedIn
case "demo <link>":
command = c.Demo
default:
log.Error().Str("subcmd", sub).Msg("Sub-command not implemented.")
exitcode = 1
Expand Down Expand Up @@ -95,6 +95,9 @@ func (c *Clotho) run(cmd SubCommand) (exitcode int) {
// table.SetAutoMergeCells(true)

switch resp.(type) {
case nil:
// empty result and nothing to do
return
case [][]string:
table.AppendBulk(resp.([][]string))
case map[string]interface{}:
Expand Down
26 changes: 21 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@ module github.com/cmj0121/clotho
go 1.20

require (
github.com/alecthomas/kong v0.8.0 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/alecthomas/kong v0.8.0
github.com/chromedp/chromedp v0.9.1
github.com/go-rod/stealth v0.4.9
github.com/olekukonko/tablewriter v0.0.5
github.com/rs/zerolog v1.29.1
)

require (
github.com/chromedp/cdproto v0.0.0-20230220211738-2b1ec77315c9 // indirect
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/go-rod/rod v0.113.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/rs/zerolog v1.29.1 // indirect
github.com/tebeka/selenium v0.9.9 // indirect
github.com/ysmood/fetchup v0.2.3 // indirect
github.com/ysmood/goob v0.4.0 // indirect
github.com/ysmood/got v0.34.1 // indirect
github.com/ysmood/gson v0.7.3 // indirect
github.com/ysmood/leakless v0.8.0 // indirect
golang.org/x/sys v0.10.0 // indirect
)
150 changes: 43 additions & 107 deletions go.sum

Large diffs are not rendered by default.

103 changes: 103 additions & 0 deletions internal/demo/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Get the Demo CLI interface.
package demo

import (
"bufio"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"

"github.com/cmj0121/clotho/internal/utils"
"github.com/rs/zerolog/log"
)

type Demo struct {
Link string `arg:"" help:"The target link."`
Action string `enum:"http,chrome" required:"" default:"http" help:"The action to perform."`

// The HTTP client for the GitHub API.
utils.Client
// The Chrome wrapper client.
utils.Chrome
}

func (d *Demo) Prologue() {
switch d.Action {
case "http":
d.Client.Prologue()
case "chrome":
d.Chrome.Prologue()
}
}

func (d *Demo) Epilogue() {
switch d.Action {
case "http":
d.Client.Epilogue()
case "chrome":
d.Chrome.Epilogue()
}
}

// Get the GitHub user information.
func (d *Demo) Execute() (data interface{}, err error) {
log.Info().Str("action", d.Action).Msg("execute the demo command")

switch d.Action {
case "http":
var resp *http.Response

resp, err = d.Client.Get(d.Link)
if err != nil {
log.Error().Err(err).Msg("failed to get the link")
return
}
defer resp.Body.Close()

var body []byte
body, err = io.ReadAll(resp.Body)
if err != nil {
log.Error().Err(err).Msg("failed to read the response body")
return
}

fmt.Println(string(body))
case "chrome":
reader := bufio.NewReader(os.Stdin)
link := d.Link
for {
d.Chrome.Navigate(link)

fmt.Print(">>> ")
text, rerr := reader.ReadString('\n')

switch rerr {
case nil:
text = strings.TrimSpace(text)

switch {
case strings.HasPrefix(text, "http://") || strings.HasPrefix(text, "https://"):
link = text
default:
fmt.Printf("invalid link: %s\n", text)
}
case io.EOF:
return
default:
log.Error().Err(rerr).Msg("failed to read the command")

err = errors.New(fmt.Sprintf("failed to read the command: %v", rerr))
return
}
}

default:
err = errors.New(fmt.Sprintf("unknown action: %v", d.Action))
return
}

return
}
138 changes: 0 additions & 138 deletions internal/linkedin/command.go

This file was deleted.

64 changes: 64 additions & 0 deletions internal/utils/chrome.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Get the global utility for the internal packages.
package utils

import (
"context"

"github.com/chromedp/chromedp"
"github.com/go-rod/stealth"
)

// The chrome wrapper with undetectable techniques.
type Chrome struct {
Headless bool `group:"chrome" help:"Run the browser in headless mode." default:"true" negatable:""`

// The ChromeDP parent context.
exec_ctx context.Context
exec_cancel context.CancelFunc

// The ChromeDP child context.
ctx context.Context
cancel context.CancelFunc
}

// open the necessary resources.
func (c *Chrome) Prologue() {
// The customized chrome options.
opts := append(chromedp.DefaultExecAllocatorOptions[:],
// general chrome options
chromedp.DisableGPU,
chromedp.NoFirstRun,
chromedp.NoDefaultBrowserCheck,
// make the browser window undetecteable for webdriver detection.
chromedp.Flag("enable-automation", false),
chromedp.Flag("disable-blink-features", "AutomationControlled"),
// headless chrome options
chromedp.Flag("headless", c.Headless),
)

c.exec_ctx, c.exec_cancel = chromedp.NewExecAllocator(context.Background(), opts...)
c.ctx, c.cancel = chromedp.NewContext(c.exec_ctx)
}

// clean up the resources.
func (c *Chrome) Epilogue() {
c.cancel()
c.exec_cancel()
}

// Navigate to the given url.
func (c *Chrome) Navigate(url string) (err error) {
err = chromedp.Run(
c.ctx,
// insert the stealth-js to the browser before the page is loaded
chromedp.Evaluate(stealth.JS, nil),
chromedp.Navigate(url),
)
return
}

// Run the sevearal actions.
func (c *Chrome) Run(actions ...chromedp.Action) (err error) {
err = chromedp.Run(c.ctx, actions...)
return
}
Loading

0 comments on commit 317d0e3

Please sign in to comment.