-
-
Notifications
You must be signed in to change notification settings - Fork 5
feat(endpoints): Added new endpoints #16
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
Open
hammad-afzall
wants to merge
6
commits into
main
Choose a base branch
from
feat/new-endpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f8aa1fa
feat(endpoints): Added new endpoints
hammad-afzall 4b93794
Merge branch 'main' into feat/new-endpoints
Lissy93 0e99e95
Merge branch 'main' into feat/new-endpoints
hammad-afzall a234574
RF: Code Refactoring and cleanup
hammad-afzall b921621
Merge branch 'feat/new-endpoints' of github.com:xray-web/web-check-ap…
hammad-afzall 04161fd
Merge branch 'main' into feat/new-endpoints
Lissy93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,156 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"math" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const archiveAPIURL = "https://web.archive.org/cdx/search/cdx" | ||
|
||
func convertTimestampToDate(timestamp string) (time.Time, error) { | ||
mask := "20060102150405" | ||
return time.Parse(mask, timestamp) | ||
} | ||
|
||
func countPageChanges(results [][]string) int { | ||
prevDigest := "" | ||
changeCount := -1 | ||
for _, curr := range results { | ||
if curr[2] != prevDigest { | ||
prevDigest = curr[2] | ||
changeCount++ | ||
} | ||
} | ||
return changeCount | ||
} | ||
|
||
func getAveragePageSize(scans [][]string) int { | ||
totalSize := 0 | ||
for _, scan := range scans { | ||
size, err := strconv.Atoi(scan[3]) | ||
if err != nil { | ||
continue | ||
} | ||
totalSize += size | ||
} | ||
return totalSize / len(scans) | ||
} | ||
|
||
func getScanFrequency(firstScan, lastScan time.Time, totalScans, changeCount int) map[string]string { | ||
formatToTwoDecimal := func(num float64) string { | ||
return fmt.Sprintf("%.2f", num) | ||
} | ||
|
||
dayFactor := lastScan.Sub(firstScan).Hours() / 24 | ||
daysBetweenScans := formatToTwoDecimal(dayFactor / float64(totalScans)) | ||
daysBetweenChanges := formatToTwoDecimal(dayFactor / float64(changeCount)) | ||
scansPerDay := formatToTwoDecimal(float64(totalScans-1) / dayFactor) | ||
changesPerDay := formatToTwoDecimal(float64(changeCount) / dayFactor) | ||
|
||
if math.IsNaN(dayFactor / float64(totalScans)) { | ||
daysBetweenScans = "0.00" | ||
} | ||
if math.IsNaN(dayFactor / float64(changeCount)) { | ||
daysBetweenChanges = "0.00" | ||
} | ||
if math.IsNaN(float64(totalScans-1) / dayFactor) { | ||
scansPerDay = "0.00" | ||
} | ||
if math.IsNaN(float64(changeCount) / dayFactor) { | ||
changesPerDay = "0.00" | ||
} | ||
|
||
return map[string]string{ | ||
"daysBetweenScans": daysBetweenScans, | ||
"daysBetweenChanges": daysBetweenChanges, | ||
"scansPerDay": scansPerDay, | ||
"changesPerDay": changesPerDay, | ||
} | ||
} | ||
|
||
func getWaybackData(url *url.URL) (map[string]interface{}, error) { | ||
cdxUrl := fmt.Sprintf("%s?url=%s&output=json&fl=timestamp,statuscode,digest,length,offset", archiveAPIURL, url) | ||
|
||
client := http.Client{ | ||
Timeout: 60 * time.Second, | ||
} | ||
|
||
resp, err := client.Get(cdxUrl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var data [][]string | ||
err = json.NewDecoder(resp.Body).Decode(&data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(data) <= 1 { | ||
return map[string]interface{}{ | ||
"skipped": "Site has never before been archived via the Wayback Machine", | ||
}, nil | ||
} | ||
|
||
if len(data) < 1 { | ||
return nil, fmt.Errorf("data slice is empty") | ||
} | ||
|
||
// Remove the header row | ||
data = data[1:] | ||
|
||
if len(data) < 1 { | ||
return nil, fmt.Errorf("data slice became empty after removing the first element") | ||
} | ||
|
||
// Access the first element of the remaining data | ||
firstScan, err := convertTimestampToDate(data[0][0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
lastScan, err := convertTimestampToDate(data[len(data)-1][0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
totalScans := len(data) | ||
changeCount := countPageChanges(data) | ||
|
||
return map[string]interface{}{ | ||
"firstScan": firstScan.Format(time.RFC3339), | ||
"lastScan": lastScan.Format(time.RFC3339), | ||
"totalScans": totalScans, | ||
"changeCount": changeCount, | ||
"averagePageSize": getAveragePageSize(data), | ||
"scanFrequency": getScanFrequency(firstScan, lastScan, totalScans, changeCount), | ||
"scans": data, | ||
"scanUrl": url, | ||
}, nil | ||
} | ||
|
||
func HandleArchives() http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
rawURL, err := extractURL(r) | ||
if err != nil { | ||
JSONError(w, ErrMissingURLParameter, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
data, err := getWaybackData(rawURL) | ||
if err != nil { | ||
http.Error(w, fmt.Sprintf("Error fetching Wayback data: %v", err), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
err = json.NewEncoder(w).Encode(data) | ||
if err != nil { | ||
http.Error(w, fmt.Sprintf("Error encoding response: %v", err), http.StatusInternalServerError) | ||
} | ||
}) | ||
} |
This file contains hidden or 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,142 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
func ResolveMx(domain string) ([]*dns.MX, int, error) { | ||
c := new(dns.Client) | ||
m := new(dns.Msg) | ||
m.SetQuestion(dns.Fqdn(domain), dns.TypeMX) | ||
r, _, err := c.Exchange(m, "8.8.8.8:53") | ||
if err != nil { | ||
return nil, dns.RcodeServerFailure, err | ||
} | ||
if r.Rcode != dns.RcodeSuccess { | ||
return nil, r.Rcode, &dns.Error{} | ||
} | ||
var mxRecords []*dns.MX | ||
for _, ans := range r.Answer { | ||
if mx, ok := ans.(*dns.MX); ok { | ||
mxRecords = append(mxRecords, mx) | ||
} | ||
} | ||
if len(mxRecords) == 0 { | ||
return nil, dns.RcodeNameError, nil | ||
} | ||
return mxRecords, dns.RcodeSuccess, nil | ||
} | ||
|
||
func ResolveTxt(domain string) ([]string, int, error) { | ||
c := new(dns.Client) | ||
m := new(dns.Msg) | ||
m.SetQuestion(dns.Fqdn(domain), dns.TypeTXT) | ||
r, _, err := c.Exchange(m, "8.8.8.8:53") | ||
if err != nil { | ||
return nil, dns.RcodeServerFailure, err | ||
} | ||
if r.Rcode != dns.RcodeSuccess { | ||
return nil, r.Rcode, &dns.Error{} | ||
} | ||
var txtRecords []string | ||
for _, ans := range r.Answer { | ||
if txt, ok := ans.(*dns.TXT); ok { | ||
txtRecords = append(txtRecords, txt.Txt...) | ||
} | ||
} | ||
if len(txtRecords) == 0 { | ||
return nil, dns.RcodeNameError, nil | ||
} | ||
return txtRecords, dns.RcodeSuccess, nil | ||
} | ||
|
||
func HandleMailConfig() http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
rawURL, err := extractURL(r) | ||
if err != nil { | ||
JSONError(w, ErrMissingURLParameter, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
mxRecords, rcode, err := ResolveMx(rawURL.Hostname()) | ||
if err != nil { | ||
JSONError(w, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if rcode == dns.RcodeNameError || rcode == dns.RcodeServerFailure { | ||
JSON(w, map[string]string{"skipped": "No mail server in use on this domain"}, http.StatusOK) | ||
return | ||
} | ||
|
||
txtRecords, rcode, err := ResolveTxt(rawURL.Hostname()) | ||
if err != nil { | ||
JSONError(w, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if rcode == dns.RcodeNameError || rcode == dns.RcodeServerFailure { | ||
JSON(w, map[string]string{"skipped": "No mail server in use on this domain"}, http.StatusOK) | ||
return | ||
} | ||
|
||
emailTxtRecords := filterEmailTxtRecords(txtRecords) | ||
mailServices := identifyMailServices(emailTxtRecords, mxRecords) | ||
|
||
JSON(w, map[string]interface{}{ | ||
"mxRecords": mxRecords, | ||
"txtRecords": emailTxtRecords, | ||
"mailServices": mailServices, | ||
}, http.StatusOK) | ||
}) | ||
} | ||
|
||
func filterEmailTxtRecords(records []string) []string { | ||
var emailTxtRecords []string | ||
for _, record := range records { | ||
if strings.HasPrefix(record, "v=spf1") || | ||
strings.HasPrefix(record, "v=DKIM1") || | ||
strings.HasPrefix(record, "v=DMARC1") || | ||
strings.HasPrefix(record, "protonmail-verification=") || | ||
strings.HasPrefix(record, "google-site-verification=") || | ||
strings.HasPrefix(record, "MS=") || | ||
strings.HasPrefix(record, "zoho-verification=") || | ||
strings.HasPrefix(record, "titan-verification=") || | ||
strings.Contains(record, "bluehost.com") { | ||
emailTxtRecords = append(emailTxtRecords, record) | ||
} | ||
} | ||
return emailTxtRecords | ||
} | ||
|
||
func identifyMailServices(emailTxtRecords []string, mxRecords []*dns.MX) []map[string]string { | ||
var mailServices []map[string]string | ||
for _, record := range emailTxtRecords { | ||
if strings.HasPrefix(record, "protonmail-verification=") { | ||
mailServices = append(mailServices, map[string]string{"provider": "ProtonMail", "value": strings.Split(record, "=")[1]}) | ||
} else if strings.HasPrefix(record, "google-site-verification=") { | ||
mailServices = append(mailServices, map[string]string{"provider": "Google Workspace", "value": strings.Split(record, "=")[1]}) | ||
} else if strings.HasPrefix(record, "MS=") { | ||
mailServices = append(mailServices, map[string]string{"provider": "Microsoft 365", "value": strings.Split(record, "=")[1]}) | ||
} else if strings.HasPrefix(record, "zoho-verification=") { | ||
mailServices = append(mailServices, map[string]string{"provider": "Zoho", "value": strings.Split(record, "=")[1]}) | ||
} else if strings.HasPrefix(record, "titan-verification=") { | ||
mailServices = append(mailServices, map[string]string{"provider": "Titan", "value": strings.Split(record, "=")[1]}) | ||
} else if strings.Contains(record, "bluehost.com") { | ||
mailServices = append(mailServices, map[string]string{"provider": "BlueHost", "value": record}) | ||
} | ||
} | ||
|
||
for _, mx := range mxRecords { | ||
if strings.Contains(mx.Mx, "yahoodns.net") { | ||
mailServices = append(mailServices, map[string]string{"provider": "Yahoo", "value": mx.Mx}) | ||
} else if strings.Contains(mx.Mx, "mimecast.com") { | ||
mailServices = append(mailServices, map[string]string{"provider": "Mimecast", "value": mx.Mx}) | ||
} | ||
} | ||
|
||
return mailServices | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to do a length check, before accessing
data[0]
anddata[len(data)-1]
to avoid potential panics?