One of the major national security concerns at the time of the founding was domestic insurrection. Several of the Federalist Papers discuss the issue:
Federalist No. 28
> THAT there may happen cases in which the national government may be necessitated to resort to force, cannot be denied.
Federalist No. 29
> THE power of regulating the militia, and of commanding its services in times of insurrection and invasion are natural incidents to the duties of superintending the common defense, and of watching over the internal peace of the Confederacy.
Article II then invests power over the militias, when called into service of the United States, in the President:
> The President shall be commander in chief of the Army and Navy of the United States, and of the militia of the several states, when called into the actual service of the United States;
In other words, the Constitution put the President singularly in charge of what the framers considered the chief tool against domestic insecurity: the nationalized militias.
Article 2, section 2 enumerates some broad powers including "Commander in Chief".
Courts have long held that protections are very different at the border. For example a police officer cannot search your car without either your consent or "reasonable suspicion" of wrong-doing. But searches of cars at border crossings are common and legal.
Government policy has long been that the border is a lot bigger than you would expect. Government officers often exceed official policy, and courts have been known to rule in their favor when they do so. See https://www.aclu.org/constitution-100-mile-border-zone for some interesting stuff.
(I have personally been stopped by the border patrol many miles from the border...)