# JSDoc: Source: net/local.js

# Source: net/local.js

    //
    // Copyright (c) 2006-2026 Wade Alcorn - wade@bindshell.net
    // Browser Exploitation Framework (BeEF) - https://beefproject.com
    // See the file 'doc/COPYING' for copying permission
    //

    /**
     * Provides networking functions for the local/internal network of the zombie.
     * @namespace beef.net.local
     */
    beef.net.local = {

    	sock: false,
    	checkJava: false,
    	hasJava: false,

    	/**
    	 * Initializes the java socket. We have to use this method because
    	 * some browsers do not have java installed or it is not accessible.
    	 * in which case creating a socket directly generates an error. So this code
    	 * is invalid:
    	 * sock: new java.net.Socket();
    	 */
    	initializeSocket: function() {
    		if(this.checkJava){
    			if(!beef.browser.hasJava()) {
    				this.checkJava=True;
    				this.hasJava=False;
    				return -1;
    			}else{
    				this.checkJava=True;
    				this.hasJava=True;
    				return 1;
    			}
    		}
    		else{
    			if(!this.hasJava) return -1;
    			else{
    				try {
    					this.sock = new java.net.Socket();
    				} catch(e) {
    					return -1;
    				}
    				return 1;
    			}
    		}
    	},

    	/**
    	 * Returns the internal IP address of the zombie.
    	 * @return {String} the internal ip of the zombie.
    	 * @error return -1 if the internal ip cannot be retrieved.
    	 */
    	getLocalAddress: function() {
    		if(!this.hasJava) return false;

    		this.initializeSocket();

    		try {
    			this.sock.bind(new java.net.InetSocketAddress('0.0.0.0', 0));
    			this.sock.connect(new java.net.InetSocketAddress(document.domain, (!document.location.port)?80:document.location.port));

    			return this.sock.getLocalAddress().getHostAddress();
    		} catch(e) { return false; }
    	},

    	/**
    	 * Returns the internal hostname of the zombie.
    	 * @return {String} the internal hostname of the zombie.
    	 * @error return -1 if the hostname cannot be retrieved.
    	 */
    	getLocalHostname: function() {
    		if(!this.hasJava) return false;

    		this.initializeSocket();

    		try {
    			this.sock.bind(new java.net.InetSocketAddress('0.0.0.0', 0));
    			this.sock.connect(new java.net.InetSocketAddress(document.domain, (!document.location.port)?80:document.location.port));

    			return this.sock.getLocalAddress().getHostName();
    		} catch(e) { return false; }
    	}

    };

    beef.regCmp('beef.net.local');