-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
240 additions
and
468 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
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 | ||
} |
This file was deleted.
Oops, something went wrong.
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,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 | ||
} |
Oops, something went wrong.