
    bh                        d dl mZmZmZ d dlmZ d dlZd dlZd dlZd dl	Z	d dl
Z
d dlZ e	j                  dd      Z e e	j                  dd            Z e e	j                  dd	            Z e	j                  d
d      Z e	j                  dd      Z ee      Z ej*                  d      ddddgZ eedeideideideidddgg ddd        ej*                  d      ZdedefdZdedefd Zd!edee   fd"Zej=                  d#dd$g%      d&        Zej=                  d'dg%      d(        Z ej=                  d)dg%      d*        Z!ed+k(  rejE                  d,d-.       yy)/    )Flaskrequestjsonify)CORSNALLOWED_ORIGINS*SHERLOCK_TIMEOUT600MAX_USERNAME_LEN30
RAPID_HOSTz(sherlock-username-checker.p.rapidapi.comRAPIDAPI_KEY zhttps://.*\.figma\.sitezhttps://www.figma.comzhttps://figma.comzhttps://checkbrandname.comzhttps://www.checkbrandname.comorigins)z/checkz/api/*z/rapid/*/healthGETOPTIONS)Content-TypezX-Requested-Withz	X-API-KeyFiQ )	resourcesmethodsallow_headerssupports_credentialsmax_agez^[A-Za-z0-9._-]+$rawreturnc                 *    | xs dj                         S )Nr   )strip)r   s    +/var/www/betterdocs.net/sherlock_api/app.pyclean_usernamer   5   s    I2    namec                 j    | syt        |       t        kD  ryt        t        j	                  |             S )NF)lenr   boolUSERNAME_REmatch)r!   s    r   is_valid_usernamer'   8   s.    
4y##!!$'((r    textc                 (   t        j                  d| xs d      }g }|D ]6  }|j                  d      }|j                  d      s&|j	                  |       8 t               }g }|D ])  }||vs|j                  |       |j	                  |       + |S )Nzhttps?://\S+r   z).,;"'<>)zhttp://https://)refindallrstrip
startswithappendsetadd)r(   urlscleaneduseenordereds         r   extract_urlsr7   ?   s    ::otzr2DG HH[!<</0NN1 5DG D=HHQKNN1 Nr    r   HEAD)r   c                       y)N)r       r;   r    r   healthr<   Q   s    r    z
/api/checkc                  z   t        t        j                  j                  dd            } t	        |       st        ddi      dfS 	 dddd	| g}t        j                  |t        j                  t        j                  d
t              }|j                  xs d|j                  rd|j                  z   ndz   }t        |      }t        d|i      dfS # t        j                  $ r t        ddt         di      dfcY S t        $ r8}t        d| t         j                  d
       t        ddi      dfcY d }~S d }~ww xY w)Nusernamer   error;Username is required and must be 1-30 chars [A-Za-z0-9._-].  python3z-mzsherlock_project.sherlockz--print-foundTstdoutstderrr(   timeout
results   zSherlock timed out after s  z[ERROR] /api/check failed: fileflushzInternal server errori  )r   r   argsgetr'   r   
subprocessrunPIPEr	   rD   rE   r7   TimeoutExpired	Exceptionprintsys)r>   commandresultoutputr2   es         r   check_usernamer\   X   s0   gll..z2>?HX&!^_`beee@d$?RZ[????$
 --%2&--$*>UWXF#	4()3..$$ X#<=M<Na!PQRTWWW @+A3/cjjM!89:C??@s%   BC *D::D:-D5/D:5D:z/rapid/checkc                     dd l } t        t        j                  j	                  dd            }t        |      st        ddi      dfS t        st        ddi      dfS t        }d	| d
| } | j                  d      xs d}|ddddddddddt         dd| dd|g}	 t        j                  |t        j                  t        j                  dd      }|j                  dk7  r3t        d|j                  j                         |j                  d      dfS |j                   dd d!ifS # t        j"                  $ r t        dd"i      d#fcY S t$        $ r8}t'        d$| t(        j                  d%       t        dd&i      dfcY d }~S d }~ww xY w)'Nr   r>   r   r?   r@   rA   z!RapidAPI not configured on serveri  r*   z/check?username=curlz/usr/bin/curlz-sSz--failz-4z	--http1.1z--connect-timeout60z
--max-time300z-Hzx-rapidapi-key: zx-rapidapi-host: zAccept: application/jsonTi,  rC   zRapidAPI proxy error)r?   detailstatusi  rI   r   zapplication/jsonzRapidAPI timeoutrK   z$[ERROR] /rapid/check (curl) failed: rL   zRapidAPI proxy unexpected error)shutilr   r   rO   rP   r'   r   	RAPID_KEYr   whichrQ   rR   rS   
returncoderE   r   rD   rT   rU   rV   rW   )rc   r>   hosturlcurl_bincmdprocr[   s           r   rapid_checkrl   z   s   gll..z2>?HX&R
  	 !DEFKKDdV+H:6Cv||F#6H%kTe ,!$((	CJ~~????
 ??a/++++-//  	  S>3E"FGG$$ ;!345s:: J4QC8szzQUV!BCDcIIJs+   A7D' D' '"FF-F FF__main__z0.0.0.0i  )rg   port)#flaskr   r   r   
flask_corsr   rQ   rc   r+   osrW   requestsgetenvr   intr	   r   r   rd   __name__appcompileALLOWED_LISTr%   strr   r$   r'   listr7   router<   r\   rl   rR   r;   r    r   <module>r|      s   ) )   	  
 "))-s3yryy!3U;< yryy!3T:;  RYY|%OP
RYY~r*	Ho
 BJJ)* $ ....	 IC$ bjj-.  )C )D )s tCy $ 9ufo. / <%)@ *@B >E7+0J ,0Jr zGGG& r    