<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
3
1g]8                 @   sD   d dl Z d dlT d dlZd dlZdZG dd dZG dd dZdS )    N)*z0.1c               @   s    e Zd ZdZdd Zdd ZdS )SchemaValidatorz+Libnftables JSON validator using jsonschemac          	   C   sJ   t jjt jjtd}t|d}tj|| _W d Q R X dd l	}|| _	d S )Nzschema.jsonrr   )
ospathjoindirname__file__openjsonloadschema
jsonschema)selfZschema_pathZschema_filer    r   /usr/lib/python3.6/nftables.py__init__   s
    zSchemaValidator.__init__c             C   s   | j j|| jd d S )N)instancer   )r   validater   )r   r   r   r   r   r   "   s    zSchemaValidator.validateN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   c               @   sP  e Zd ZdZdddddddd	ZdWdXdYdZd[d\d]d^d_d`dadbdZdZdcddZdd Zdd Z	dd Z
dd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Zd?d@ ZdAdB ZdCdD ZdEdF ZdGdH Z dIdJ Z!dKdL Z"dMdN Z#dOdP Z$dQdR Z%dSdT Z&dUdV Z'dS )dNftablesz*A class representing libnftables interface                   @   )scannerparserevalZnetlinkZmnlz	proto-ctxZsegtreer               	   
      )
reversednsservice	statelesshandler   echoguidnumeric_protonumeric_prionumeric_symbolnumeric_timeterseNlibnftables.so.1.1.0c             C   s>  t j|}|j| _t| j_tg| j_|j| _t| j_tg| j_|j	| _	ttg| j	_|j
| _
t| j
_tg| j
_|j| _ttg| j_|j| _t| j_tg| j_|j| _t| j_tg| j_|j| _t| j_tg| j_|j| _t| j_tg| j_|j| _t| j_ttg| j_|j| _tg|j_| jd| _| j| j | j| j dS )al  Instantiate a new Nftables class object.

        Accepts a shared object file to open, by default standard search path
        is searched for a file named 'libnftables.so'.

        After loading the library using ctypes module, a new nftables context
        is requested from the library and buffering of output and error streams
        is turned on.
        r   N)ZcdllZLoadLibraryZnft_ctx_newZc_void_pZrestypeZc_intZargtypesnft_ctx_output_get_flagsZc_uintnft_ctx_output_set_flagsnft_ctx_output_get_debugnft_ctx_output_set_debugZnft_ctx_buffer_outputnft_ctx_get_output_bufferZc_char_pZnft_ctx_buffer_errornft_ctx_get_error_buffernft_run_cmd_from_buffernft_ctx_free_Nftables__ctx)r   Zsofilelibr   r   r   r   C   sD    









zNftables.__init__c             C   s   | j | j d S )N)r>   r?   )r   r   r   r   __del__   s    zNftables.__del__c             C   s   | j | }| j| j|@ S )N)output_flagsr7   r?   )r   nameflagr   r   r   Z__get_output_flag   s    
zNftables.__get_output_flagc             C   sD   | j | }| j| j}|r$||B }n
|| @ }| j| j| ||@ S )N)rB   r7   r?   r8   )r   rC   valrD   flagsZ	new_flagsr   r   r   Z__set_output_flag   s    


zNftables.__set_output_flagc             C   s
   | j dS )zGet the current state of reverse DNS output.

        Returns a boolean indicating whether reverse DNS lookups are performed
        for IP addresses in output.
        r+   )_Nftables__get_output_flag)r   r   r   r   get_reversedns_output   s    zNftables.get_reversedns_outputc             C   s   | j d|S )zEnable or disable reverse DNS output.

        Accepts a boolean turning reverse DNS lookups in output on or off.

        Returns the previous value.
        r+   )_Nftables__set_output_flag)r   rE   r   r   r   set_reversedns_output   s    zNftables.set_reversedns_outputc             C   s
   | j dS )zGet the current state of service name output.

        Returns a boolean indicating whether service names are used for port
        numbers in output or not.
        r,   )rG   )r   r   r   r   get_service_output   s    zNftables.get_service_outputc             C   s   | j d|S )zEnable or disable service name output.

        Accepts a boolean turning service names for port numbers in output on
        or off.

        Returns the previous value.
        r,   )rI   )r   rE   r   r   r   set_service_output   s    zNftables.set_service_outputc             C   s
   | j dS )zGet the current state of stateless output.

        Returns a boolean indicating whether stateless output is active or not.
        r-   )rG   )r   r   r   r   get_stateless_output   s    zNftables.get_stateless_outputc             C   s   | j d|S )zEnable or disable stateless output.

        Accepts a boolean turning stateless output either on or off.

        Returns the previous value.
        r-   )rI   )r   rE   r   r   r   set_stateless_output   s    zNftables.set_stateless_outputc             C   s
   | j dS )z~Get the current state of handle output.

        Returns a boolean indicating whether handle output is active or not.
        r.   )rG   )r   r   r   r   get_handle_output   s    zNftables.get_handle_outputc             C   s   | j d|S )zEnable or disable handle output.

        Accepts a boolean turning handle output on or off.

        Returns the previous value.
        r.   )rI   )r   rE   r   r   r   set_handle_output   s    zNftables.set_handle_outputc             C   s
   | j dS )zzGet the current state of JSON output.

        Returns a boolean indicating whether JSON output is active or not.
        r   )rG   )r   r   r   r   get_json_output   s    zNftables.get_json_outputc             C   s   | j d|S )zEnable or disable JSON output.

        Accepts a boolean turning JSON output either on or off.

        Returns the previous value.
        r   )rI   )r   rE   r   r   r   set_json_output   s    zNftables.set_json_outputc             C   s
   | j dS )zzGet the current state of echo output.

        Returns a boolean indicating whether echo output is active or not.
        r/   )rG   )r   r   r   r   get_echo_output   s    zNftables.get_echo_outputc             C   s   | j d|S )zEnable or disable echo output.

        Accepts a boolean turning echo output on or off.

        Returns the previous value.
        r/   )rI   )r   rE   r   r   r   set_echo_output   s    zNftables.set_echo_outputc             C   s
   | j dS )zGet the current state of GID/UID output.

        Returns a boolean indicating whether names for group/user IDs are used
        in output or not.
        r0   )rG   )r   r   r   r   get_guid_output   s    zNftables.get_guid_outputc             C   s   | j d|S )zEnable or disable GID/UID output.

        Accepts a boolean turning names for group/user IDs on or off.

        Returns the previous value.
        r0   )rI   )r   rE   r   r   r   set_guid_output   s    zNftables.set_guid_outputc             C   s
   | j dS )ztGet current status of numeric protocol output flag.

        Returns a boolean value indicating the status.
        r1   )rG   )r   r   r   r   get_numeric_proto_output  s    z!Nftables.get_numeric_proto_outputc             C   s   | j d|S )zSet numeric protocol output flag.

        Accepts a boolean turning numeric protocol output either on or off.

        Returns the previous value.
        r1   )rI   )r   rE   r   r   r   set_numeric_proto_output  s    z!Nftables.set_numeric_proto_outputc             C   s
   | j dS )zzGet current status of numeric chain priority output flag.

        Returns a boolean value indicating the status.
        r2   )rG   )r   r   r   r   get_numeric_prio_output  s    z Nftables.get_numeric_prio_outputc             C   s   | j d|S )zSet numeric chain priority output flag.

        Accepts a boolean turning numeric chain priority output either on or
        off.

        Returns the previous value.
        r2   )rI   )r   rE   r   r   r   set_numeric_prio_output  s    z Nftables.set_numeric_prio_outputc             C   s
   | j dS )zsGet current status of numeric symbols output flag.

        Returns a boolean value indicating the status.
        r3   )rG   )r   r   r   r   get_numeric_symbol_output%  s    z"Nftables.get_numeric_symbol_outputc             C   s   | j d|S )zSet numeric symbols output flag.

        Accepts a boolean turning numeric representation of symbolic constants
        in output either on or off.

        Returns the previous value.
        r3   )rI   )r   rE   r   r   r   set_numeric_symbol_output,  s    z"Nftables.set_numeric_symbol_outputc             C   s
   | j dS )zqGet current status of numeric times output flag.

        Returns a boolean value indicating the status.
        r4   )rG   )r   r   r   r   get_numeric_time_output6  s    z Nftables.get_numeric_time_outputc             C   s   | j d|S )zSet numeric times output flag.

        Accepts a boolean turning numeric representation of time values
        in output either on or off.

        Returns the previous value.
        r4   )rI   )r   rE   r   r   r   set_numeric_time_output=  s    z Nftables.set_numeric_time_outputc             C   s
   | j dS )z|Get the current state of terse output.

        Returns a boolean indicating whether terse output is active or not.
        r5   )rG   )r   r   r   r   get_terse_outputG  s    zNftables.get_terse_outputc             C   s   | j d|S )zEnable or disable terse output.

        Accepts a boolean turning terse output either on or off.

        Returns the previous value.
        r5   )rI   )r   rE   r   r   r   set_terse_outputN  s    zNftables.set_terse_outputc             C   sV   | j | j}g }x2| jj D ]$\}}||@ r|j| || M }qW |rR|j| |S )zmGet currently active debug flags.

        Returns a set of flag names. See set_debug() for details.
        )r9   r?   debug_flagsitemsappend)r   rE   namesnvr   r   r   	get_debugW  s    

zNftables.get_debugc             C   s`   | j  }t|ttgkr|g}d}x*|D ]"}t|tkrB| j| }||O }q(W | j| j| |S )a  Set debug output flags.

        Accepts either a single flag or a set of flags. Each flag might be
        given either as string or integer value as shown in the following
        table:

        Name      | Value (hex)
        -----------------------
        scanner   | 0x1
        parser    | 0x2
        eval      | 0x4
        netlink   | 0x8
        mnl       | 0x10
        proto-ctx | 0x20
        segtree   | 0x40

        Returns a set of previously active debug flags, as returned by
        get_debug() method.
        r   )rg   typestrintra   r:   r?   )r   valuesoldrE   rf   r   r   r   	set_debugh  s    

zNftables.set_debugc             C   sd   d}t |tsd}|jd}| j| j|}| j| j}| j| j}|rZ|jd}|jd}|||fS )a  Run a simple nftables command via libnftables.

        Accepts a string containing an nftables command just like what one
        would enter into an interactive nftables (nft -i) session.

        Returns a tuple (rc, output, error):
        rc     -- return code as returned by nft_run_cmd_from_buffer() fuction
        output -- a string containing output written to stdout
        error  -- a string containing output written to stderr
        FTzutf-8)
isinstancebytesencoder=   r?   r;   r<   decode)r   ZcmdlineZcmdline_is_unicodercoutputerrorr   r   r   cmd  s    



zNftables.cmdc             C   sJ   | j d}| jtj|\}}}|s.| j | t|r@tj|}|||fS )ai  Run an nftables command in JSON syntax via libnftables.

        Accepts a hash object as input.

        Returns a tuple (rc, output, error):
        rc     -- return code as returned by nft_run_cmd_from_buffer() function
        output -- a hash object containing library standard output
        error  -- a string containing output written to stderr
        T)rR   ru   r   dumpslenloads)r   	json_rootZjson_out_oldrr   rs   rt   r   r   r   json_cmd  s    



zNftables.json_cmdc             C   s   | j st | _ | j j| dS )zValidate JSON object against libnftables schema.

        Accepts a hash object as input.

        Returns True if JSON is valid, raises an exception otherwise.
        T)	validatorr   r   )r   ry   r   r   r   json_validate  s    zNftables.json_validater   r   r   r   r   r   r          i   i   i   )r6   )(r   r   r   r   ra   rB   r{   r   rA   rG   rI   rH   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   rg   rm   ru   rz   r|   r   r   r   r   r   %   sl   
<
	
						


	#r   )r   Zctypessysr   ZNFTABLES_VERSIONr   r   r   r   r   r   <module>   s   